2

I've wrote a application which should starts at windows startup. I've add a entry in windows register in HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. Entry has been added successfully, but program is not starting properly.

I've tested application on Windows 7 64 bits. Application needs to have admin permissions to run, maybe this is the reason why it is not starting?

I saw also that the entry's value is not in the quotation marks, but the others are. Is it compulsory?

Here is my c# code:

            var registry = Registry.CurrentUser;
            var key = registry.OpenSubKey(runKeyBase, true);
            key.SetValue(KEY, directory + @"\" + filename);
            Registry.CurrentUser.Flush();

How cant I make it working?

nosbor
  • 2,826
  • 3
  • 39
  • 63

3 Answers3

5

Why not just place a shortcut in the Startup folder? That way you can also set the properties of the shortcut to run as admin

Edit:

Navigate to the exe you are looking to run on startup and right click, create shortcut.

In the properties for that shortcut, check run as administrator.

Then place this in the startup folder (you can get there by clicking Explore on the folder in the start menu). This will start that application at windows login. If the UAC requires approval it will prompt the user if it is ok to run the program.

JeremyK
  • 1,075
  • 1
  • 22
  • 45
  • can you give us more details on this? now, clicking the run as admin property, does this mean your exe does not need a manifest file if you do this? also, will this actually work when UAC is fully enabled (at it's default setting). Will it work in both Vista and Win7 with UAC at its default settings? thanks. – Erx_VB.NExT.Coder Jul 09 '12 at 00:42
  • Navigate to the exe you are looking to run on startup and right click, create shortcut. In the properties for that shortcut, check run as administrator. Then place this in the startup folder (you can get there by clicking Explore on the folder in the start menu). This will start that application at windows login. If the UAC requires approval it will prompt the user if it is ok to run the program. – JeremyK Jul 09 '12 at 15:37
  • 1
    ah yes, the startup folder is a neat trick, and of course auto-embedding the run as admin option into the short cut is another great idea, this way you dont need a manifest file - neat trick to use anyone who is interested. Upvoted. – Erx_VB.NExT.Coder Sep 01 '12 at 12:31
3

As far as I see this is due to user access control settings that allows only signed applications to start else it will ask for Administrator permissions.

Due to this during startup, OS will simply not run the application even if you have done registry settings.

Also quotation are not mandatory. You can either have them or not.

The way I did was to place a shortcut in the Startup folder. Registry settings will not work.

Additionally One thing you can try is place the file in /system32 or /windows and then try setting in registry.

Dinesh
  • 3,652
  • 2
  • 25
  • 35
  • can you give us more details on this? now, clicking the run as admin property, does this mean your exe does not need a manifest file if you do this? also, will this actually work when UAC is fully enabled (at it's default setting). Will it work in both Vista and Win7 with UAC at its default settings? also, about the system32 & windows dirs, does this actually work? so, i can get elevated mode on my first run (no problem) then can do elevated registry changes, but on my restarted/second run, UAC blocks my program. will making autostart location in these dirs solve the problem? – Erx_VB.NExT.Coder Jul 09 '12 at 00:44
  • When you run application as an admin, the UAC won't prompt and this does not require any manifest file to be embedded. Manifest file is required when you want to set the premissions to run the application by simply double clicking. If you double click or simply open and manifest file contains runasAdmin set then windows will automatically ask for elevation. The same happerned to me first time it ran and then failed the other times... I think you need to sign the application if you want to run it at startup but i never tried signing my personal app since i don't want to pay for that :) – Dinesh Jul 09 '12 at 11:39
  • nice idea, also, have you tried using Windows Task Scheduler to get rid of the Prompt? Don't know why WIndows has given developers and users this backdoor, and it exists and its on purpose. upvoted. – Erx_VB.NExT.Coder Sep 01 '12 at 12:33
0

You could self-elevatte the program at startup. Just execute the following code at the beginning:

public static void runAsAdmin(string[] args)
    {
        ProcessStartInfo proc = new ProcessStartInfo();

        if (args != null)
            proc.Arguments = string.Concat(args);

        proc.UseShellExecute = true;
        proc.WorkingDirectory = Environment.CurrentDirectory;
        proc.FileName = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
        proc.Verb = "runas";



        bool isElevated;
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

        if (!isElevated)
        {

            try
            {
                Process.Start(proc);
            }
            catch
            {
                //No Admin rights, continue without them
                return;
            }
            //Close current process for switching to elevated one
            Environment.Exit(0);
        }
        return;
    }

Also after getting Admin rights you could disable UAC notifications(If it was enabled) for silent start in the future:

private void disableUAC()
    {
        RegistryKey regKey = null;

        try
        {
            regKey = Registry.LocalMachine.OpenSubKey(ControlServiceResources.UAC_REG_KEY, true);
        }

        catch (Exception e)
        {
            //Error accessing registry
        }


        try
        {
            regKey.SetValue("ConsentPromptBehaviorAdmin", 0);
        }
        catch (Exception e)
        {
            //Error during Promt disabling
        }


    }