天天看點

C#軟體試用天數小程式

....

     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); //建立鍵值,存儲可使用次數

   }

繼續閱讀