C#開發-基礎知識及有用技巧
1、時間長度的計算 TimeSpan類。
例如:TimeSpan span = dateTime1 - dateTime2 方便啊
2、從類(Class)返回一個System.Type類型,用typeof關鍵字
3、從一個對象實例(Object)返回一個System.Type類型,用GetType方法
4、判斷是否處于設計狀態:DesignMode屬性
5、根據GUID創建對象實例
System.Guid pGuid = new Guid(guid); System.Type ObjectCustorm = Type.GetTypeFromCLSID(pGuid); Object obj = Activator.CreateInstance(ObjectCustorm);
ControlPaint.DrawReversibleFrame、ControlPaint.DrawReversibleLine方法
7、獲取Enum類型中的所有枚舉值:
Enum.GetNames方法
將字符串轉換成枚舉值
Enum.Parse方法
8、Label放在圖片上時,使Label透明
picLogo.Controls.Add(lblStatus); lblStatus.BackColor = Color.Transparent;
9、調用幫助文件
打開幫助文件
Help.ShowHelp(this,@"c:/windows/help/mspaint.chm");
打開幫助文件,并跳轉到指定的主題
Help.ShowHelp(this,@"c:/windows/help/mspaint.chm","paint_lines.htm");
打開幫助文件,并轉到“索引”選項卡
Help.ShowHelpIndex(this,@"c:/windows/help/mspaint.chm","paint_lines.htm");
在屏幕上顯示一條浮動的幫助信息
Help.ShowPopup(this,"這里是幫助信息",new Point(100,100));
10、通過AppDomain在應用程序之間傳遞數據
例如,兩個系統可能會共用登錄信息,登錄一個系統后,再啟動另一個系統時,不需要重新登錄。
先定義一個在應用程序之間傳遞的數據的類,該類必須從MarshalByRefObject繼承:
/// <summary> /// 用于在不同的appdomain之間傳遞參數 /// </summary> public class AppDomainInfo:MarshalByRefObject { public int UserID; }
AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationName = "測試程序"; AppDomain appDomain = AppDomain.CreateDomain("TestDomain", null, setup); AppDomainInfo domainInfo = new AppDomainInfo(); domainInfo.UserID = Winsharp.BaseClass.AppConfigInfo.UserID; appDomain.SetData("domainInfo",domainInfo); object obj = appDomain.CreateInstanceFromAndUnwrap(str,"TestDomain.Test"); (obj as Form).Show();
API中有GetTickCount函數,C#中為Environment.TickCount
12、取得安裝操作系統輸入的用戶姓名和公司名稱:
Microsoft.Win32.RegistryKey cmicRegKey=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software"); cmicRegKey=cmicRegKey.OpenSubKey("Microsoft"); cmicRegKey=cmicRegKey.OpenSubKey("MS Setup (ACME)"); cmicRegKey=cmicRegKey.OpenSubKey("User Info"); object cmicCompany = cmicRegKey.GetValue("DefCompany"); object cmicUser = cmicRegKey.GetValue("DefName");