winform通过注册表限制软件使用次数

1.创建注册表文件:
打开记事本,输入一些内容:

REGEDIT4

[HKEY_CURRENT_USER\Software\MyRegDataApp]
"UseTime"="10"


保存为“RegData.reg”

2.创建winform项目

引用名称空间

1using Microsoft.Win32 ;

在Form中激活load事件,并添加代码

 1RegistryKey RootKey,RegKey;    
 2
 3            //项名为:HKEY_CURRENT_USER\Software
 4            RootKey = Registry.CurrentUser.OpenSubKey ("Software",true);
 5            
 6            //打开子项:HKEY_CURRENT_USER\Software\MyRegDataApp
 7            if ((RegKey = RootKey.OpenSubKey ("MyRegDataApp",true)) == null)
 8            {
 9                RootKey.CreateSubKey("MyRegDataApp");//不存在,则创建子项
10                RegKey = RootKey.OpenSubKey ("MyRegDataApp",true);
11                RegKey.SetValue ("UseTime",(object)9);    //创建键值,存储可使用次数
12                MessageBox.Show ("您可以免费使用本软件10次!","感谢您首次使用");
13                return;
14            }

15
16            try 
17            {
18                object usetime = RegKey.GetValue ("UseTime");//读取键值,可使用次数
19                MessageBox.Show ("你还可以使用本软件 :"+ usetime.ToString ()+ "次!","确认",MessageBoxButtons.OK ,MessageBoxIcon.Information );
20                int newtime = Int32.Parse (usetime.ToString()) -1;
21
22                if (newtime<0)
23                {
24                    if (MessageBox.Show ("继续使用,请购买本软件!","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information )== DialogResult.OK )
25                    {
26                        Application.Exit ();
27                    }

28                }

29                else
30                {
31                    RegKey.SetValue ("UseTime",(object)newtime);//更新键值,可使用次数减1
32                }

33            }

34            catch
35            {
36                RegKey.SetValue ("UseTime",(object)10);    //创建键值,存储可使用次数
37                MessageBox.Show ("您可以免费使用本软件10次!","感谢您首次使用");
38                return;
39            }

3.运行效果






posted @ 2007-06-06 13:49  东明  阅读(1172)  评论(0)    收藏  举报