代码检查我的申请30天试用期

时间:2014-04-06 12:41:45

标签: c# trial trialware time-trial

大家好我已经使用c#开发了一个应用程序,我希望将其使用限制在30天,30天后用户不能使用该应用程序。我在线获得了以下代码。我应该使用什么代码添加elseif(天数< 30)以限制其使用

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion

namespace Coder24
{
    class TrialTimeManager
    {
        /// <summary>
        /// A Temporary variable.
        /// </summary>
        private string temp = "";

        /// <summary>
        /// The constructor.
        /// </summary>
        public TrialTimeManager()
        {

        }

        /// <summary>
        /// Sets the new date +31 days add for trial.
        /// </summary>
        public void SetNewDate()
        {
            DateTime newDate = DateTime.Now.AddDays(31);
            temp = newDate.ToLongDateString();
            StoreDate(temp);
        }

        /// <summary>
        /// Checks if expire or NOT.
        /// </summary>
        public void Expired()
        {
            string d = "";
            using (Microsoft.Win32.RegistryKey key =
                Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Test"))
            {
                 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){
                /* Fill with more code, once it has expired, what will happen nex! */
            }
        }

        /// <summary>
        /// Stores the new date value in registry.
        /// </summary>
        /// <param name="value"></param>
        private void StoreDate(string value)
        {
            try
            {
                using (Microsoft.Win32.RegistryKey key =
                    Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Test"))
                {
                    key.SetValue("Date", value, Microsoft.Win32.RegistryValueKind.String);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这完全取决于您希望在试用期结束时发生的事情。

您可能想立即结束该计划。您也可以选择访问您的网站购买许可证。也许是这样的:

if (day > 30)
{
    if (MessageBox.Show("Trial expired. Visit site to purchase license?",
        "Trial Expired!", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        Process.Start("http://www.yourwebsite.com");
    }

    Environment.Exit(0);
}

如果您还有其他要求,请使用更多详细信息更新您的问题。