c0nfus3d
c0nfus3d

Reputation: 1450

Run C# Application on Windows Start up

I'm trying to start a few applications for the current user only when Windows starts up.

I can accomplish this with the following:

RegistryKey oKey = Registry.CurrentUser.OpenSubKey("Software", true);
oKey = oKey.OpenSubKey("Microsoft", true);
oKey = oKey.OpenSubKey("Windows", true);
oKey = oKey.OpenSubKey("CurrentVersion", true);
oKey = oKey.OpenSubKey("Run", true);
oKey.SetValue("Application 1", "C:\\path\\to\\ap1.exe");
oKey.SetValue("Application 2", "C:\\path\\to\\ap2.exe");

But I'm trying to run this as part of a Visual Studio Installer Project. I've added my custom action, the program starts like it should, and the installer works great in XP.

In Windows 7, the installer gets elevated privileges, and does everything but insert the entries into the registry for the current user. How ever, it does insert the registry entries when ran as a standalone application (outside of the installer project) and it does not get elevated privileges.

The only thing I can figure is that with the elevated privileges, it's trying to insert the entries into the Administrators account instead of the current user? or is there something else I'm missing? and is there another way I can achieve my goal here?

Upvotes: 7

Views: 691

Answers (2)

Frazell Thomas
Frazell Thomas

Reputation: 6111

Is there a reason to not use the startup folder for the user?

More than likely the problem is the user the installer is executing under. If the user isn't the admin the elevated installer will run under the context of the user who elevated the process.

It would be a safer choice to add your application to the startup folder or to add the registry key on first launch.

Upvotes: 2

Christopher Painter
Christopher Painter

Reputation: 55601

If the installer is getting elevated permissions, why write the setting to HKCU? Write it to HKLM instead. It will then take effect for all users.

Upvotes: 1

Related Questions