本文實例講述了WinForm通過操作注冊表實現限制軟件使用次數的方法。分享給大家供大家參考,具體如下:
1.創建注冊表文件:
打開記事本,輸入一些內容:
REGEDIT4[HKEY_CURRENT_USER/Software/MyRegDataApp]"UseTime"="10"
保存為“RegData.reg”
2.創建winform項目
引用名稱空間
using Microsoft.Win32 ;
在Form中激活load事件,并添加代碼
RegistryKey RootKey,RegKey;//項名為:HKEY_CURRENT_USER/SoftwareRootKey = Registry.CurrentUser.OpenSubKey ("Software",true);//打開子項:HKEY_CURRENT_USER/Software/MyRegDataAppif ((RegKey = RootKey.OpenSubKey ("MyRegDataApp",true)) == null){ RootKey.CreateSubKey("MyRegDataApp");//不存在,則創建子項 RegKey = RootKey.OpenSubKey ("MyRegDataApp",true); RegKey.SetValue ("UseTime",(object)9); //創建鍵值,存儲可使用次數 MessageBox.Show ("您可以免費使用本軟件10次!","感謝您首次使用"); return;}try{ object usetime = RegKey.GetValue ("UseTime");//讀取鍵值,可使用次數 MessageBox.Show ("你還可以使用本軟件 :"+ usetime.ToString ()+ "次!","確認",MessageBoxButtons.OK ,MessageBoxIcon.Information ); int newtime = Int32.Parse (usetime.ToString()) -1; if (newtime<0) { if (MessageBox.Show ("繼續使用,請購買本軟件!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information )== DialogResult.OK ) { Application.Exit (); } } else { RegKey.SetValue ("UseTime",(object)newtime);//更新鍵值,可使用次數減1 }}catch{ RegKey.SetValue ("UseTime",(object)10); //創建鍵值,存儲可使用次數 MessageBox.Show ("您可以免費使用本軟件10次!","感謝您首次使用"); return;}更多關于C#相關內容感興趣的讀者可查看本站專題:《WinForm控件用法總結》、《C#窗體操作技巧匯總》、《C#數據結構與算法教程》、《C#常見控件用法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
新聞熱點
疑難解答