C#程序員經(jīng)常用到的10個(gè)實(shí)用代碼片段
1 讀取操作系統(tǒng)和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系統(tǒng)中,輸出以下信息
- 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數(shù)量,內(nèi)存容量
可以通過(guò)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;
 - }
 
請(qǐng)?zhí)砑訉?duì)程序集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 讀取注冊(cè)表鍵值對(duì)
- 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));
 - }
 - }
 
請(qǐng)?zhí)砑用臻gMicrosoft.Win32,以確保上面的代碼可以編譯。
4 啟動(dòng),停止Windows服務(wù)
這項(xiàng)API提供的實(shí)用功能常常用來(lái)管理應(yīng)用程序中的服務(wù),而不必到控制面板的管理服務(wù)中進(jìn)行操作。
- ServiceController controller = new ServiceController(“e-M-POWER”);
 - controller.Start();
 - if (controller.CanPauseAndContinue)
 - {
 - controller.Pause();
 - controller.Continue();
 - }
 - controller.Stop();
 
.net提供的API中,可以實(shí)現(xiàn)一句話安裝與卸載服務(wù)
- if (args[0] == "/i")
 - {
 - ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
 - }
 - else if (args[0] == "/u")
 - {
 - ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
 - }
 
如代碼所示,給應(yīng)用程序傳入i或u參數(shù),以表示是卸載或是安裝程序。
#p#
5 驗(yàn)證程序是否有strong name (P/Invoke)
比如在程序中,為了驗(yàn)證程序集是否有簽名,可調(diào)用如下方法
- [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);
 
這個(gè)功能常用在軟件保護(hù)方法,可用來(lái)驗(yàn)證簽名的組件。即使你的簽名被人去掉,或是所有程序集的簽名都被去除,只要程序中有這一項(xiàng)調(diào)用代碼,則可以停止程序運(yùn)行。
6 響應(yīng)系統(tǒng)配置項(xiàng)的變更
比如我們鎖定系統(tǒng)后,如果QQ沒(méi)有退出,則它會(huì)顯示了忙碌狀態(tài)。
請(qǐng)?zhí)砑用臻gMicrosoft.Win32,然后對(duì)注冊(cè)下面的事件。
- . DisplaySettingsChanged (包含Changing) 顯示設(shè)置
 - . InstalledFontsChanged 字體變化
 - . PaletteChanged
 - . PowerModeChanged 電源狀態(tài)
 - . SessionEnded (用戶正在登出或是會(huì)話結(jié)束)
 - . SessionSwitch (變更當(dāng)前用戶)
 - . TimeChanged 時(shí)間改變
 - . UserPreferenceChanged (用戶偏號(hào) 包含Changing)
 
我們的ERP系統(tǒng),會(huì)監(jiān)測(cè)系統(tǒng)時(shí)間是否改變,如果將時(shí)間調(diào)整后ERP許可文件之外的范圍,會(huì)導(dǎo)致ERP軟件不可用。
7 運(yùn)用Windows7的新特性
Windows7系統(tǒng)引入一些新特性,比如打開(kāi)文件對(duì)話框,狀態(tài)欄可顯示當(dāng)前任務(wù)的進(jìn)度。
- Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog ofd = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
 - ofd.AddToMostRecentlyUsedList = true;
 - ofd.IsFolderPicker = true;
 - ofd.AllowNonFileSystemItems = true;
 - ofd.ShowDialog();
 
用這樣的方法打開(kāi)對(duì)話框,與BCL自帶類庫(kù)中的OpenFileDialog功能更多一些。不過(guò)只限于Windows 7系統(tǒng)中,所以要調(diào)用這段代碼,還要檢查操作系統(tǒng)的版本要大于6,并且添加對(duì)程序集Windows API Code Pack for Microsoft®.NET Framework的引用,請(qǐng)到這個(gè)地址下載 http://code.msdn.microsoft.com/WindowsAPICodePack
8 檢查程序?qū)?nèi)存的消耗
用下面的方法,可以檢查.NET給程序分配的內(nèi)存數(shù)量
- 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);
 
在我的系統(tǒng)中,它運(yùn)行的結(jié)果如下所示
- Before allocations: 651,064
 - After allocations: 40,690,080
 
使用下面的方法,可以檢查當(dāng)前應(yīng)用程序占用的內(nèi)存
- 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 使用記秒表檢查程序運(yùn)行時(shí)間
如果你擔(dān)憂某些代碼非常耗費(fèi)時(shí)間,可以用StopWatch來(lái)檢查這段代碼消耗的時(shí)間,如下面的代碼所示
- 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);
 
現(xiàn)在已經(jīng)有專門的工具來(lái)檢測(cè)程序的運(yùn)行時(shí)間,可以細(xì)化到每個(gè)方法,比如dotNetPerformance軟件。
以上面的代碼為例子,您需要直接修改源代碼,如果是用來(lái)測(cè)試程序,則有些不方便。請(qǐng)參考下面的例子。
- class AutoStopwatch : System.Diagnostics.Stopwatch, IDisposable
 - {
 - public AutoStopwatch()
 - {
 - Start();
 - }
 - public void Dispose()
 - {
 - Stop();
 - Console.WriteLine(“Elapsed: {0}”, this.Elapsed);
 - }
 - }
 
借助于using語(yǔ)法,像下面的代碼所示,可以檢查一段代碼的運(yùn)行時(shí)間,并打印在控制臺(tái)上。
- using (new AutoStopwatch())
 - {
 - Decimal total2 = 0;
 - int limit2 = 1000000;
 - for (int i = 0; i < limit2; ++i)
 - {
 - total2 = total2 + (Decimal)Math.Sqrt(i);
 - }
 - }
 
10 使用光標(biāo)
當(dāng)程序正在后臺(tái)運(yùn)行保存或是冊(cè)除操作時(shí),應(yīng)當(dāng)將光標(biāo)狀態(tài)修改為忙碌??墒褂孟旅娴募记?。
- 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;
 - }
 - }
 
用法如下所示,這個(gè)寫法,是為了預(yù)料到程序可能會(huì)拋出異常
- using (new AutoWaitCursor(this))
 - {
 - ...
 - throw new Exception();
 - }
 
如代碼所示,即使拋出異常,光標(biāo)也可以恢復(fù)到之間的狀態(tài)。
















 
 
 


 
 
 
 