1

Who can help? I need the program to throw itself into startup. I tried a lot that did not help for example:

There is one more news that my exe file does not start via cmd (command line); any other application launches mine no I write so start the path to the program I tried on d and c \ .exe

Through the code

Registry.CurrentUser.CreateSubKey (@ "Software \ Microsoft \ Windows \ CurrentVersion \ Run;

(does not work) it just doesn't start, it's as if it is there and it is on it doesn't start it I tried it through the other folder and through the USB flash drive evenly!

I tried to copy it to the startup folder manually, it launches fine, it starts up and everything works, but it can't throw its own shortcut saying that there are no admin rights, in the manifest it changed the launch line from the admin to the normal one and back when it is from the admin even when I manually threw it. I tried to move files through bad

The examples below do not work either.

RegistryKey rk = Registry.CurrentUser.OpenSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); rk.SetValue ("FileName", "FileName.exe");

If you manually place the full path to the program in HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run - does it work?

My code is:

public bool SetAutorunValue(bool autorun)
{
    string ExePath = Application.ExecutablePath;
    RegistryKey reg;
    reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    if (autorun)
    {
        if (reg.GetValue(NameApp) == null)
        {
            reg.SetValue(NameApp, ExePath);
            reg.Close();
            return true;
        }
        else
            return false;
    }
    else
    {
        if (reg.GetValue(NameApp) != null)
        {
            reg.DeleteValue(NameApp);
            reg.Close();
            return true;
        }
        else
            return false;
    }
}

1 Answers1

0

I use below snippet and it works fine.

public void AddprogramToStartUp()
{
    try
    {
        Log.Info("Checking if app is added to windows startup");

        string appName = "TestApplication";
        RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        var val = Convert.ToString(rk.GetValue(appName));
        if (string.IsNullOrEmpty(val))
        {
            rk.SetValue(appName, Application.ExecutablePath);
            Log.Info("Added!");
        }
        else
            Log.Info("Already added!");
    }
    catch(Exception e)
    {
        Log.Error("Failed to add/check windows startup", e);
    }
}
vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • This code works fine. Change your menifest file to have `RequestedExecutionLevel` to `AsInvoker`. If you have UAC enabled. Check what is changing your menifest file to back and forth. – vendettamit Dec 12 '19 at 17:06
  • I have an ent manifest file There is one more news that my exe file does not start via cmd (command line); any other application launches mine no I write so start the path to the program I tried on d and c \ .exe – Никита Dec 12 '19 at 17:19
  • something else to complement cmd shortcut launches the command line and exe does not want to what – Никита Dec 12 '19 at 17:32