C#程式員經常用到的10個實用程式碼片段

來源:互聯網
上載者:User

 1 讀取作業系統和CLR的版本

OperatingSystem os = System.Environment.OSVersion;Console.WriteLine(“Platform: {0}”, os.Platform);Console.WriteLine(“Service Pack: {0}”, os.ServicePack);Console.WriteLine(“Version: {0}”, os.Version);Console.WriteLine(“VersionString: {0}”, os.VersionString);Console.WriteLine(“CLR Version: {0}”, System.Environment.Version);

  在我的Windows 7系統中,輸出以下資訊

  Platform: Win32NT
  Service Pack:
  Version: 6.1.7600.0
  VersionString: Microsoft Windows NT 6.1.7600.0
  CLR Version: 4.0.21006.1

  2 讀取CPU數量,記憶體容量

  可以通過Windows Management Instrumentation (WMI)提供的介面讀取所需要的資訊。

private static UInt32 CountPhysicalProcessors(){     ManagementObjectSearcher objects = new ManagementObjectSearcher(        “SELECT * FROM Win32_ComputerSystem”);     ManagementObjectCollection coll = objects.Get();     foreach(ManagementObject obj in coll)    {        return (UInt32)obj[“NumberOfProcessors”];    }     return 0;}private static UInt64 CountPhysicalMemory(){   ManagementObjectSearcher objects =new ManagementObjectSearcher(      “SELECT * FROM Win32_PhysicalMemory”);   ManagementObjectCollection coll = objects.Get();   UInt64 total = 0;   foreach (ManagementObject obj in coll)   {       total += (UInt64)obj[“Capacity”];    }    return total;}

請添加對程式集System.Management的引用,確保代碼可以正確編譯。

Console.WriteLine(“Machine: {0}”, Environment.MachineName);Console.WriteLine(“# of processors (logical): {0}”, Environment.ProcessorCount);Console.WriteLine(“# of processors (physical): {0}”  CountPhysicalProcessors());Console.WriteLine(“RAM installed: {0:N0} bytes”,  CountPhysicalMemory());Console.WriteLine(“Is OS 64-bit? {0}”,   Environment.Is64BitOperatingSystem);Console.WriteLine(“Is process 64-bit? {0}”,  Environment.Is64BitProcess);Console.WriteLine(“Little-endian: {0}”, BitConverter.IsLittleEndian);foreach (Screen screen in  System.Windows.Forms.Screen.AllScreens){     Console.WriteLine(“Screen {0}”, screen.DeviceName);     Console.WriteLine(“\tPrimary {0}”, screen.Primary);     Console.WriteLine(“\tBounds: {0}”, screen.Bounds);     Console.WriteLine(“\tWorking Area: {0}”,screen.WorkingArea);     Console.WriteLine(“\tBitsPerPixel: {0}”,screen.BitsPerPixel);}

  3 讀取註冊表索引值對

using (RegistryKey keyRun = Registry.LocalMachine.OpenSubKey(@”Software\Microsoft\Windows\CurrentVersion\Run”)){    foreach (string valueName in keyRun.GetValueNames())    {     Console.WriteLine(“Name: {0}\tValue: {1}”, valueName, keyRun.GetValue(valueName));    }}

  請添加命名空間Microsoft.Win32,以確保上面的代碼可以編譯。

  4 啟動,停止Windows服務

  這項API提供的實用功能常常用來管理應用程式中的服務,而不必到控制台的管理服務中進行操作。

ServiceController controller = new ServiceController(“e-M-POWER”);      controller.Start();      if (controller.CanPauseAndContinue)      {          controller.Pause();          controller.Continue();      }      controller.Stop();

  .net提供的API中,可以實現一句話安裝與卸載服務

if (args[0] == "/i") {       ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); } else if (args[0] == "/u") {   ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); }

如代碼所示,給應用程式傳入i或u參數,以表示是卸載或是安裝程式。

  5 驗證程式是否有strong name (P/Invoke)

  比如在程式中,為了驗證程式集是否有簽名,可調用如下方法

[DllImport("mscoree.dll", CharSet=CharSet.Unicode)]static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool  pfWasVerified);bool notForced = false;bool verified = StrongNameSignatureVerificationEx(assembly, false, ref notForced);Console.WriteLine("Verified: {0}\nForced: {1}", verified, !notForced);

  這個功能常用在軟體保護方法,可用來驗證簽名的組件。即使你的簽名被人去掉,或是所有程式集的簽名都被去除,只要程式中有這一項調用代碼,則可以停止程式運行。

  6 響應系統配置項的變更

  比如我們鎖定系統後,如果QQ沒有退出,則它會顯示了忙碌狀態。

  請添加命名空間Microsoft.Win32,然後對註冊下面的事件。

  . DisplaySettingsChanged (包含Changing) 顯示設定
  . InstalledFontsChanged 字型變化
  . PaletteChanged
  . PowerModeChanged 電源狀態
  . SessionEnded (使用者正在登出或是會話結束)
  . SessionSwitch (變更目前使用者)
  . TimeChanged 時間改變
  . UserPreferenceChanged (使用者偏號 包含Changing)

  我們的ERP系統,會監測系統時間是否改變,如果將時間調整後ERP許可檔案之外的範圍,會導致ERP軟體不可用。

7 運用Windows7的新特性

  Windows7系統引入一些新特性,比如開啟檔案對話方塊,狀態列可顯示當前任務的進度。

Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog ofd = new  Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();ofd.AddToMostRecentlyUsedList = true;ofd.IsFolderPicker = true;ofd.AllowNonFileSystemItems = true;ofd.ShowDialog();

  用這樣的方法開啟對話方塊,與BCL內建類庫中的OpenFileDialog功能更多一些。不過只限於Windows 7系統中,所以要調用這段代碼,還要檢查作業系統的版本要大於6,並且添加對程式集Windows API Code Pack for Microsoft.NET Framework的引用,請到這個地址下載 http://code.msdn.microsoft.com/WindowsAPICodePack

  8 檢查程式對記憶體的消耗

  用下面的方法,可以檢查.NET給程式分配的記憶體數量

long available = GC.GetTotalMemory(false);Console.WriteLine(“Before allocations: {0:N0}”, available);int allocSize = 40000000;byte[] bigArray = new byte[allocSize];available = GC.GetTotalMemory(false);Console.WriteLine(“After allocations: {0:N0}”, available);

  在我的系統中,它啟動並執行結果如下所示

Before allocations: 651,064After allocations: 40,690,080

  使用下面的方法,可以檢查當前應用程式佔用的記憶體

Process proc = Process.GetCurrentProcess();Console.WriteLine(“Process Info: “+Environment.NewLine+
“Private Memory Size: {0:N0}”+Environment.NewLine +“Virtual Memory Size: {1:N0}” + Environment.NewLine +
“Working Set Size: {2:N0}” + Environment.NewLine +“Paged Memory Size: {3:N0}” + Environment.NewLine +“Paged System Memory Size: {4:N0}” + Environment.NewLine +
“Non-paged System Memory Size: {5:N0}” + Environment.NewLine,proc.PrivateMemorySize64,   proc.VirtualMemorySize64,  proc.WorkingSet64,  proc.PagedMemorySize64, proc.PagedSystemMemorySize64,  proc.NonpagedSystemMemorySize64 );

9 使用記秒錶檢查程式已耗用時間

  如果你擔憂某些代碼非常耗費時間,可以用StopWatch來檢查這段代碼消耗的時間,如下面的代碼所示

System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();timer.Start();Decimal total = 0;int limit = 1000000;for (int i = 0; i < limit; ++i){      total = total + (Decimal)Math.Sqrt(i);}timer.Stop();Console.WriteLine(“Sum of sqrts: {0}”,total);Console.WriteLine(“Elapsed milliseconds: {0}”,timer.ElapsedMilliseconds);Console.WriteLine(“Elapsed time: {0}”, timer.Elapsed);

  現在已經有專門的工具來檢測程式的已耗用時間,可以細化到每個方法,比如dotNetPerformance軟體。

  以上面的代碼為例子,您需要直接修改原始碼,如果是用來測試程式,則有些不方便。請參考下面的例子。

class AutoStopwatch : System.Diagnostics.Stopwatch, IDisposable{   public AutoStopwatch()   {        Start();   }   public void Dispose()   {       Stop();       Console.WriteLine(“Elapsed: {0}”, this.Elapsed);   }}

  藉助於using文法,像下面的代碼所示,可以檢查一段代碼的已耗用時間,並列印在控制台上。

using (new AutoStopwatch()){    Decimal total2 = 0;    int limit2 = 1000000;    for (int i = 0; i < limit2; ++i)    {       total2 = total2 + (Decimal)Math.Sqrt(i);    }}

  10 使用游標

  當程式正在後台運行儲存或是冊除操作時,應當將游標狀態修改為忙碌。可使用下面的技巧。

class AutoWaitCursor : IDisposable{private Control _target;private Cursor _prevCursor = Cursors.Default;public AutoWaitCursor(Control control){   if (control == null)   {     throw new ArgumentNullException(“control”);   }   _target = control;   _prevCursor = _target.Cursor;   _target.Cursor = Cursors.WaitCursor;}public void Dispose(){   _target.Cursor = _prevCursor;}}

  用法如下所示,這個寫法,是為了預料到程式可能會拋出異常

using (new AutoWaitCursor(this)){...throw new Exception();}

  如代碼所示,即使拋出異常,游標也可以恢複到之間的狀態。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.