C# 代码片段

StringBuilder拼接小技巧

            Stopwatch watch = new Stopwatch();
            watch.Start();
            var sb = new StringBuilder();
            for (int i = 0; i < 1000000; i++)
            {
                sb.Append("1").Append("1");//这种写法更快
                sb.AppendFormat("{0}{1}",0,1);
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);

时间格式转换

string strDateFormat = "yyyyMMddHHmmss";
      string date = txtTime.Value;
      DateTime convertTime = DateTime.ParseExact(date, strDateFormat, new System.Globalization.CultureInfo("zh-CN"), System.Globalization.DateTimeStyles.AllowWhiteSpaces);
      Response.Write(string.Format("{0}", convertTime.ToString()));

 字符串编码

  使用 System.Web.HttpUtility.UrlEncode   对字符串进行编码。
  内存中的字符串默认编码是UTF-8。

C# 调用C++编写的ocx控件

使用命令行 aximp 将其编译成dll,如MyActiveX.ocx,编译后,则有AxMYACTIVEXLib.dll,MYACTIVEXLib.dll。
 本次是使用控制台程序调用该控件,添加引用AxMYACTIVEXLib.dll,然后
  AxMyActiveX my = new AxMyActiveX();
  my.CreateControl();//无界面程序,必须强制实例化控件才可以调用其方法,还需添加System.Windows.Forms引用
  my.DownloadPic(2, @"E:新建文件夹", "18800", "Provider=OraOLEDB.Oracle;Data Source=HWITDB;User Id=system;Password=whhw;"); 
注意:使用.net的命令行工具,生成的是4.0的dll,如需要使用低版本编译,则可以  C:Program FilesMicrosoft SDKsWindowsv7.0Ain 去寻找低版本的exe。

 清空MemoryStream 

                ms.SetLength(0);
                ms.Position = 0;

 解决安装程序挂起

        public static bool DeleteSessionManager()
        {
            try
            {
                RegistryKey LocalMachine = Registry.LocalMachine;
                RegistryKey system = LocalMachine.OpenSubKey("SYSTEM", true);
                RegistryKey CurrentControlSet = system.OpenSubKey("CurrentControlSet", true);
                RegistryKey Control = CurrentControlSet.OpenSubKey("Control", true);
                RegistryKey SessionManager = Control.OpenSubKey("Session Manager", true);
                string[] subkeyName = SessionManager.GetValueNames();
                foreach (string str in subkeyName)
                {
                    if (str == "PendingFileRenameOperations")
                    {
                        //删除键值PendingFileRenameOperations
                        SessionManager.DeleteValue("PendingFileRenameOperations");
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
原文地址:https://www.cnblogs.com/codealone/p/3440639.html