WinForm通过操作注册表实现限制软件使用次数的方法

1.创建注册表文件:

打开记事本,输入一些内容:

?
1
2
3
REGEDIT4
[HKEY_CURRENT_USER /Software/MyRegDataApp ]
"UseTime" = "10"

保存为“RegData.reg”

2.创建winform项目

引用名称空间

?
1
using Microsoft.Win32 ;

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
RegistryKey RootKey,RegKey;
//项名为:HKEY_CURRENT_USER/Software
RootKey = Registry.CurrentUser.OpenSubKey ( "Software" , true );
//打开子项:HKEY_CURRENT_USER/Software/MyRegDataApp
if ((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 ;
}

转载于:https://www.cnblogs.com/xiaohua19920/p/9821454.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值