0

My goal is to have my application run on startup.

My problem is my application is not writing to 'Run' in regedit.

I have this code

RegistryKey rWrite = Registry.CurrentUser.OpenSubKey(@"HKey_Current_User\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

and I believe what this is suppose to do is write my application to

HKey_Current_User\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\run

However, this is not writing anything to regedit.

Things to note:

My application forces the user to run in administrator. It essentially checks if they have ran in administrator and if they haven't it displays a messagebox then closes the program with

Environment.Exit(0);
Zong
  • 6,160
  • 5
  • 32
  • 46
conjure
  • 55
  • 7
  • Did you actually `write` to it? You got the Key but did you write it? – wingerse May 01 '16 at 20:34
  • As you seem to have ruled out permission problems this is most likely a result of registry virtualization where your 32 bit process writes to a different place than you expect on a 64 bit system. If this is true this question should be closed as a duplicate of http://stackoverflow.com/questions/10533421/accessing-64-bit-registry-from-32-bit-application – Martin Liversage May 01 '16 at 20:38

1 Answers1

4

You got the Key with write access (Probably does not exist because you got CurrentUser inside CurrentUser), but you are not setting any values inside. If you want your program to start, you need to set your application path inside. Here's how you would do it:

var rWrite = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

rWrite.SetValue("YourOwnKeyForYourApp",
                  AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName);
wingerse
  • 3,670
  • 1
  • 29
  • 61
  • RegistryKey is not a type which is not valid in the given context, – conjure May 01 '16 at 20:45
  • Oh yes, my bad. I have changed it to `YourOwnKeyForYourApp` which should be a string. You can write any string you want as a key for your app there. – wingerse May 01 '16 at 20:46