作者: admin

  • 从WebBrowser控件中获取CookieContainer

    从WebBrowser控件中获取CookieContainer

    private CookieContainer _cookie = new CookieContainer();
    private void GetCookie()
    {
        if (webBrowser1.Document.Cookie != null)
        {
            string cookieStr = webBrowser1.Document.Cookie;
            string[] cookstr = cookieStr.Split( ';');
            foreach ( string str in cookstr)
            {
                string[] cookieNameValue = str.Split( '=');
                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                ck.Domain = ".qq.com";
                _cookie.Add(ck);
            }
        }
    }
    C#
  • 从CookieContainer中获取所有Cookie

    从CookieContainer中获取所有Cookie:

    /// <summary>
    /// 把CookieContainer所有的Cookie读出来
    /// </summary>
    /// <param name="cc"></param>
    /// <returns></returns>
    private List <Cookie > GetAllCookies(CookieContainer cc)
    {
        List<Cookie> lstCookies = new List< Cookie>();
        Hashtable table = ( Hashtable)cc.GetType().InvokeMember( "m_domainTable",
            System.Reflection. BindingFlags.NonPublic | System.Reflection.BindingFlags .GetField |
            System.Reflection. BindingFlags.Instance, null , cc, new object [] { });
    
        foreach (object pathList in table.Values)
        {
            SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list" ,
                System.Reflection. BindingFlags.NonPublic | System.Reflection.BindingFlags .GetField
                | System.Reflection. BindingFlags.Instance, null , pathList, new object[] { });
            foreach ( CookieCollection colCookies in lstCookieCol.Values)
                foreach ( Cookie c in colCookies)
                    lstCookies.Add(c);
        }
    
        return lstCookies;
    }
    
    
    ///////////////////////////////////////////////////以下是使用样例代码:
    private string GetSkey()
    {
        string skey = string.Empty;
    
        GetCookie();
        List<Cookie> listCookie = GetAllCookies(_cookie);
        foreach (Cookie c in listCookie)
        {
            if (c.Name == "skey")
            {
                skey = c.Value;
                break;
            }
        }
        return skey;
    }
    
    C#
  • 关于Linux上的链接库

    Linux上的链接库的知识:

    .a是静态库,.so是动态库;a是archive的缩写,so是shared object的缩写

  • lua的.NET实现: NeoLua

    NeoLua 可以让你在 .NET 的应用中使用 Lua 语言或者反过来(当前支持的 Lua 版本是 5.2),其目的是遵循 C-Lua 实现并且合并完整的 .NET 框架支持。你可以很方便在 Lua 程序中调用 .NET 的 functions/classes/interfaces/events ,同时也可以轻松在 .NET 应用中调用 Lua 的变量和函数。

    github: https://github.com/neolithos/neolua

    codeplex: https://neolua.codeplex.com/

  • Django安装过程

    Django安装过程

    1. 在官网下载安装包,并解压
    2. 找到setup.py所在的目录,执行python setup.py install
    3. 上面的命令执行完成,django 就安装完了,安装目录在%Phthon的安装目录%\Lib\site-packages
    4. 运行程序  python manage.py runserver

    检查django版本:

    import django
    Python
    django.VERSION
    Python

    python -c "import django; print(django.get_version())"
    Bash
  • django1.7  在windows上 运行 python manage.py runserver 启动网站时报错

    django1.7  在windows上 运行 python manage.py runserver 启动网站时报错。

    错误信息:django.db.utils.OperationalError: unable to open database file

    网上都说是权限问题,结果加上权限还是不行。

    最后发现路径中有中文,尝试换到全英文路径启动,结果问题解决。

  • 关于异常处理的感悟

    关于异常处理的感悟:

    把异常都放到最外层的代码处理,封装的类中不要拦截任何异常,类库如果需要对异常进行处理,可以处理后再抛出,不要拦截。

    这样方便最外层代码可以灵活的处理各种情况。

  • WinForm listView自动滚动

    WinForm listView自动滚动,使用EnsureVisible可以让listView控件自动滚动。

    如下:

    ListViewItem item = new ListViewItem (info.CompanyName); //这个是第一行第一列
    item.SubItems.Add(info.ZiHao); //第一行第二列
    item.SubItems.Add(info.Pinyin);
    item.SubItems.Add(info.Attribute);
    listView1.Items.Add(item);
    listView1.EnsureVisible(item.Index); //自动滚动
    C#
  • SQLite时间操作

    获取当前时间:

    select CURRENT_TIMESTAMP;  –以格林尼治标准时间为基准

    select datetime('now');  –以格林尼治标准时间为基准

    获取本地当前时间:

    select datetime('now', 'localtime');
    SQL
    -- 将UNIX时间戳转化为时间日期格式
    SELECT datetime(1092941466, 'unixepoch');
    SQL
    -- 将UNIX时间戳转化为本地时间
    SELECT datetime(1092941466, 'unixepoch', 'localtime');
    SQL

    获取时间差,以秒为单位,格林尼治标准时间为基准

    select strftime('%s',datetime('now')) - strftime('%s','2015-03-03 19:50:00');
    SQL

    获取时间差,以天为单位,当前时区时间为基准

    select strftime('%J', datetime('now', 'localtime')) - strftime('%J','2015-03-03 20:08:00');
    SQL
  • C#使用NPOI导出Excel

    C#使用NPOI导出Excel

    使用代码如下:

    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.IO;
    using System.Text;
    
    using NPOI.HSSF.UserModel;
    using NPOI.HPSF;
    using NPOI.POIFS.FileSystem;
    using NPOI.SS.UserModel;
    
    namespace Common
    {
        public class NPOIHelper
        {
            public void Export(System.Windows.Forms. ListView listView, string fileName)
            {
                var hssfworkbook = new HSSFWorkbook();
                ////create a entry of DocumentSummaryInformation
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "NPOI Team";
                hssfworkbook.DocumentSummaryInformation = dsi;
    
                ////create a entry of SummaryInformation
                SummaryInformation si = PropertySetFactory .CreateSummaryInformation();
                si.Subject = "NPOI SDK Example";
                hssfworkbook.SummaryInformation = si;
    
    
    
                //调整列宽
                var sheet1 = hssfworkbook.CreateSheet( "Sheet1");
                sheet1.SetColumnWidth(0, 10000);
                sheet1.SetColumnWidth(1, 10000);
                sheet1.SetColumnWidth(2, 10000);
                sheet1.SetColumnWidth(3, 10000);
    
                //表头
                var row0 = sheet1.CreateRow(0);
                row0.CreateCell(0).SetCellValue( "企业名称" );
                row0.CreateCell(1).SetCellValue( "中文字号" );
                row0.CreateCell(2).SetCellValue( "中文拼音" );
                row0.CreateCell(3).SetCellValue( "行业特征" );
    
                for( int i=0; i<listView.Items.Count; i++)
                {
                    string a = listView.Items[i].SubItems[0].Text;
                    string b = listView.Items[i].SubItems[1].Text;
                    string c = listView.Items[i].SubItems[2].Text;
                    string d = listView.Items[i].SubItems[3].Text;
                    var row = sheet1.CreateRow(i+1); //如果不使用表头,这里就不用+1了
                    row.CreateCell(0).SetCellValue(a);
                    row.CreateCell(1).SetCellValue(b);
                    row.CreateCell(2).SetCellValue(c);
                    row.CreateCell(3).SetCellValue(d);
                }
    
    
                //Write the stream data of workbook to the root directory
                //MemoryStream file = new MemoryStream();
               // hssfworkbook.Write(file);
    
                //写入文件
                FileStream file = new FileStream (fileName, FileMode.Create);
                hssfworkbook.Write(file);
                file.Close();
         
    
            }
        }
    }
    
    C#

    调用:

    注意先using Common;

    /*保存对话框*/
    SaveFileDialog saveFileDialog = new SaveFileDialog ();
    saveFileDialog.Filter = "导出Excel(*.xls)|*.xls";
    
    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
        NPOIHelper npoi = new NPOIHelper();
        npoi.Export(this.listView1, saveFileDialog.FileName);
        MessageBox.Show( "导出完成!" );
                  
    }
    C#