将日期信息添加到注册表并检索它 [英] Adding date Information to the registry and retrieving it

查看:84
本文介绍了将日期信息添加到注册表并检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace Thermocoders_Licensing_System
{
  class TrialTimeManager
  {
     private string temp = "";

      public TrialTimeManager() { }

  public void SetNewDate()
  {
    DateTime newDate = DateTime.Now.AddDays(31);
    temp = newDate.ToLongDateString();
    StoreDate(temp);
  }

public void Expired()
{
    string d = "";
    using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyApp"))
    {
        d = (String)key.GetValue("Date");
    }
    DateTime now = DateTime.Parse(d);
    int day = (now.Subtract(DateTime.Now)).Days;
    if (day > 30) { }
    else if (0 < day && day <= 30)
    {
        string daysLeft = string.Format("{0} days more to expire", now.Subtract(DateTime.Now).Days);
        MessageBox.Show(daysLeft);

    }
    else if (day <= 0)
    {
        MessageBox.Show("Trial Expired Please Purchase The Product");
    }
}

public void StoreDate(string value)
{
    try
    {
        using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MyApp"))
        {
            key.SetValue("Date", value, Microsoft.Win32.RegistryValueKind.String);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

}

}









大家好,



我从代码项目找到了上面的代码。它工作正常,但是从注册表中存储和检索日期信息时出现问题。例如,如果我在2014年5月1日运行应用程序,则到期日期正确设置为2014年6月1日。



以上代码工作正常,它正确地显示了我更改系统时间的日期说如果我将日期更改为2014年2月2日,它将显示剩余29天。现在发生的事情是每当我设定日期说从2014年5月1日到2014年6月1日,而不是抛出试用期已过期的错误,它一直说30天剩余



和当我将日期从1月5日设置为6月1日时,存储在注册表中的日期会不断变化,并且会在当前日期之前30天设置到期日期,这应该不会发生。可能是什么错误?



任何人都可以帮我解决这个问题吗?



谢谢





Hi Everyone,

I found the above code from code project.It was working fine but there was an issue while storing and retrieving date information from the registry.For eg If I run the application say on 1st May 2014,the expiration date is correctly set to 1st june 2014.

The above code was working fine and it properly displayed the date when I changed my system time say If I change the date to 2nd may 2014 ,it would display 29days remaining.Now what is happening is that whenever I set the date say from 1st may 2014 to 1st june 2014, instead of throwing an error saying "Trial Period has expired" , it keeps saying 30 days remaining

And when I set the date from 1st may to 1st june,The date stored in the registry keeps changing and it sets the expiration date 30days ahead of the current date which should not be happening. What could be the error?

Can Anyone help me fix this?

Thanks

推荐答案

首先,请不要使用注册表。

Vista及以上版本的注册表需要管理员权限才能修改,所以每次调用SetNewDate你应用程序将需要阿联酋访问,这在以后的版本中只会变得更加严格。



相反,创建一个许可证文件并将其存储在更容易访问的地方:我应该在哪里存储我的数据? [ ^ ]解释了更好的位置。



我会看看你自己存储的内容:首先在StoreDate方法中放置一个断点,然后查看调用它的频率,以及使用什么值。
First off, don't use the registry.
The registry in Vista and above need admin access to modify, so every time you call SetNewDate you app will require UAE access, and this can only get more rigid in future versions.

Instead, create a licence file and store it somewhere more accessible: Where should I store my data?[^] explains better locations.

I'd look at what you stored myself: start with putting a breakpoint in the StoreDate method and see how often it is called, and with what values.


这篇关于将日期信息添加到注册表并检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆