-5

i'm developing an application with c#. I'd like to run the application on startup without having administrator privileges. I managed to run it on startup by copying the application itself on the startup folder but it required administrator privileges to do it. Could you please tell me how i may do it without administrator privileges?

Thanks and sorry for bad english, i'm not native

Andreawu98
  • 45
  • 1
  • 6
  • Obviously doing that is a security issue, hence the requirement for admin access. Your question also has nothing to do with C# – KiwiPiet Dec 01 '15 at 19:31
  • For a specific user (i.e. *current user only*): [Run Program At Windows StartUp](http://stackoverflow.com/a/7478989/1441) – crashmstr Dec 01 '15 at 19:32
  • There's no way to do what you want from within the code of your application. The end user must take action. – Brian Driscoll Dec 01 '15 at 19:33

2 Answers2

3

As a non-admin, you can only set something to startup automatically for your own user account. You would use the per-user startup folder (or one of the HKCU registry keys) for this.

FOLDERID_CommonStartup

For all users, requires admin privileges to modify. Location depends on OS version and customization, but by default is either: %ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\StartUp %ALLUSERSPROFILE%\Start Menu\Programs\StartUp

For C#, you can retrieve the path with Environment.GetFolderPath(SpecialFolder.CommonStartup).

FOLDERID_Startup

Per-user startup. Location depends on OS version and customization, but by default is either: %APPDATA%\Microsoft\Windows\Start Menu\Programs\StartUp %USERPROFILE%\Start Menu\Programs\StartUp

For C#, you can retrieve the path with Environment.GetFolderPath(SpecialFolder.Startup). You'd normally want to place a shortcut to your app here, but there is no managed API for this so you'll need to pinvoke or have your installer create one for you.

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

For all users, requires admin privileges to modify. For C#, can add an entry using the static Microsoft.Win32.Registry class.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Per user. To add a new entry:

const string HKCU = "HKEY_CURRENT_USER";
const string RUN_KEY = @"SOFTWARE\\Microsoft\Windows\CurrentVersion\Run";
string exePath = System.Windows.Forms.Application.ExecutablePath;
Microsoft.Win32.Registry.SetValue(HKCU + "\\" + RUN_KEY, "AppName", exePath);
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
-1

The closest you can get to automating this kind of this is to create an installer that installs your application as a service. The "user" that runs the service is defined in the service entry itself, and that can be an admin if the current user is not. It also gives your server team the ability to revoke privileges on that user should they decide your application is a security problem.

There are some things you need to do in your C# program to satisfy the requirements of being a service component found here:

https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

Note that this will probably not work with a start-up program that requires a GUI of any sort (Winform, WPF, etc).