Problem Reading And Writing Registry In C#
-
Hi guys, I have this code in my form1_load event:
RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise"); string licenseCo = licenseCheck.GetValue("tlb").ToString(); DateTime licenseDate = Convert.ToDateTime(licenseCo); if (licenseDate.AddDays(30) == System.DateTime.Now) { MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); }
now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:
RegistryKey licenseKey;
licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
licenseKey.SetValue("tlb",DateTime.Now);
Registry.LocalMachine.Flush();However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.
-
Hi guys, I have this code in my form1_load event:
RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise"); string licenseCo = licenseCheck.GetValue("tlb").ToString(); DateTime licenseDate = Convert.ToDateTime(licenseCo); if (licenseDate.AddDays(30) == System.DateTime.Now) { MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); }
now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:
RegistryKey licenseKey;
licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
licenseKey.SetValue("tlb",DateTime.Now);
Registry.LocalMachine.Flush();However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.
Hi, some remarks: 1. exceptions tend to contain a lot of information, however you need to look at all of it, i.e. use Exception.ToString() instead of Exception.Message one of the pieces of information it offers is a filename and linenumber; that would point straight to the culprit line. Also teach your IDE to always show line numbers in edit windows, see here[^]. 2. Recent Windows versions don't allow a regular user to access/modify system settings that would extend to other users; the LocalMachine registry hive would be in that category. 3. Your expiration test needs a less than comparison, not an equals. DateTimes seldom are equal as they contain information up to the tick (one tenth of a microsecond). :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi guys, I have this code in my form1_load event:
RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise"); string licenseCo = licenseCheck.GetValue("tlb").ToString(); DateTime licenseDate = Convert.ToDateTime(licenseCo); if (licenseDate.AddDays(30) == System.DateTime.Now) { MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); }
now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:
RegistryKey licenseKey;
licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
licenseKey.SetValue("tlb",DateTime.Now);
Registry.LocalMachine.Flush();However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.
-
Hi guys, I have this code in my form1_load event:
RegistryKey licenseCheck = Registry.LocalMachine.OpenSubKey(@"software/openorganise"); string licenseCo = licenseCheck.GetValue("tlb").ToString(); DateTime licenseDate = Convert.ToDateTime(licenseCo); if (licenseDate.AddDays(30) == System.DateTime.Now) { MessageBox.Show("Your OpenOrganise License Has Expired. Please Purchase A New One.", "OpenOrganise Message.", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); }
now, ive got a config value that sets it to write to a file if its the first time run or not, etc etc, this works fine, in the event of a first run i have:
RegistryKey licenseKey;
licenseKey = Registry.LocalMachine.CreateSubKey(@"Software\openorganise");
licenseKey.SetValue("tlb",DateTime.Now);
Registry.LocalMachine.Flush();However, when running the application i get: "object referance not set to a instance of a object" Im stumped to as why. I even went and manually made the reg key. thanks, Ben.
-
Ben Magee wrote:
OpenSubKey(@"software/openorganise");
Ben Magee wrote:
CreateSubKey(@"Software\openorganise");
Should these both be backslash? Alan.
-
Thanks for the feedback, I will look into all these issues now. Could you please give a example of a lessthan statement in the current context im using? thanks, Ben.
if (1 < 2) MesageBox("one is less than two");
:doh:
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Ben Magee wrote:
OpenSubKey(@"software/openorganise");
Ben Magee wrote:
CreateSubKey(@"Software\openorganise");
Should these both be backslash? Alan.
-
After changing my registry edits to CurrentUser and changing to backslashes im still getting my exception error. I also changed
System.DateTime.Now;
to
system.DateTime.Today;
would that work any better? Cheers, Ben.
use the line numbers as I've told you, and learn to debug your code yourself. This thread already is a lot longer than the code it is about... :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
After changing my registry edits to CurrentUser and changing to backslashes im still getting my exception error. I also changed
System.DateTime.Now;
to
system.DateTime.Today;
would that work any better? Cheers, Ben.
Ben Magee wrote:
would that work any better?
Would what work? And better how? You need to show the extract of the code where the problem occurs, and the full description or text of the exception. The question above really makes almost no sense whatever, and even less in the context of your original question. Read through a few more of the posts here to get an idea of the sort of questions that get resolved quickly.