Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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);
            }
        
        }

    }

}


Hello Everyone,

I found the above code here on CodeProject and works fine.The problem that I was facing was regarding access to the Registry.When I run the above code,it gives me an error saying access to registry key is denied.But it works fine when I run it with administrator privileges.
I want that the user should be able to run the program without Administrator Rights.How can that be achieved?.I read articles online about User Access Control(UAC) but couldn't find a solution.

My second doubt was about disabling application usage after 30 days.The code above keeps a track of number of days remaining for the trial period to expire.I want that after 30 days get over,the user should not be able to use the application and when he/she changes the system clock ,it should be detected and application usage should be denied.
can anyone provide me with the code to achieve the same?

Thanks,
Posted
Comments
[no name] 3-May-14 8:33am    
Accessing registry in _LocalMachine_ section needs always admin rights
Maciej Los 3-May-14 8:53am    
Sounds like an answer ;)
BTW: only write access needs admin rights
[no name] 3-May-14 9:00am    
You are right, only writing needs admin rights. But I think it is too trivial to be the answer :)
Maciej Los 3-May-14 9:04am    
The best answer is short answer even if looks trivial ;)
[no name] 3-May-14 9:21am    
Afraid to get downvoted ;)

1 solution

You cannot access the registry for writing without admin rights - this was a deliberate security change made with the release of Vista, as a result of the abuse which the registry got in previous versions, and the damage a rogue program could do. You can access it for read without problems.

I wouldn't use the registry: Where should I store my data?[^] explains somewhere easier to access. I'd use a GUID folder name in the Environment.SpecialFolder.CommonApplicationData folder, and store my licence file there - encrypted or more likely hashed.

You can't prevent the user from altering the system clock: that can be done via the BIOS on power up if he really wants to, and the disk containing the operating system hasn't even been spun up to speed yet by the time that occurs!

The only reliable way to prevent the user exceeding the 30 day limit is to require a check with an internet based site to validate a licence key each time. And that gives you other problems which are likely to annoy more real users than prevent any piracy.
I would suggest that you don;t spend too much time on primitive security: think about how muany copies you expect to sell, and the profit on each - then work out how much you will likely lose to piracy. Compare that with the cost of your time, and it doesn't take much before you have spent a lot more than you will save...and annoy one customer with an overzealous security system, and you have lot that customer for ever!
 
Share this answer
 
Comments
Maciej Los 3-May-14 9:16am    
+5
[no name] 3-May-14 9:30am    
also my not counting 5 ;)
vivek murli 4-May-14 3:47am    
Thanks OriginalGriff :)
OriginalGriff 4-May-14 4:09am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900