conjure
conjure

Reputation: 55

Writing Run (on startup) registry key

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

Upvotes: 0

Views: 1843

Answers (1)

wingerse
wingerse

Reputation: 3796

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

Upvotes: 4

Related Questions