-2

Hi everyone I have developed an application using c# and I wish to restrict its use to 30days and after 30 days the user should not be able to use the application.I got the following code online.What code should I add in elseif(days<30) to restrict its use

#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);
            }
        }
    }
}
user2614235
  • 161
  • 3
  • 7
  • 21
  • Well, what do you want to happen when the 30 days have expired? – O. R. Mapper Apr 06 '14 at 12:46
  • as I mentioned in the question the user should not be able to use the application – user2614235 Apr 06 '14 at 13:56
  • There is a wide range of options on how to accomplish this. Given the answer you accepted, it seems like your question is entirely unrelated to a 30-days-period (or any time limit at all); you are actually asking *how to make an application unusable when it cannot be used as a trial anymore*. – O. R. Mapper Apr 06 '14 at 14:02
  • @O.R.Mapper your point is valid.Yes the user should not be able to use it.So what is to be done? – user2614235 Apr 06 '14 at 14:12
  • [Grant Winney](http://stackoverflow.com/users/301857/grant-winney) already provides a good suggestion. But ultimately, the decision is up to you; *you* have to know how your application is supposed to behave. "not usable" is way too broad, as that can be everything from immediately closing the application, over disabling all, or at least essential, controls (so the application can still be looked at, but not used productively), to displaying nag messages upon every button click (so everything is still working, but practically unusuable unless the user wants to get insane). – O. R. Mapper Apr 06 '14 at 14:41
  • O.R.Mapper to be precise I want to close the application immediately after 30 days gets over and yes @Grant Winney's solution is also good ie going to a website .. – user2614235 Apr 06 '14 at 14:45

1 Answers1

0

This depends entirely on what you want to have happen when a trial expires.

You probably want to immediately end the program. You could also give them the option of visiting your website to purchase a license. Maybe something like this:

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);
}

If you have something else in mind, or other requirements, update your question with more details.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165