软件编程
位置:首页>> 软件编程>> C#编程>> WinForm通过操作注册表实现限制软件使用次数的方法

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

作者:songkexin  发布时间:2023-07-27 15:39:57 

标签:WinForm,注册表

本文实例讲述了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/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;
}

希望本文所述对大家C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com