Rifat
Rifat

Reputation: 1888

setting console app as startup program sets dll as startup instead of exe

I have a simple console app and I am setting it to run on startup, but it sets the startup path as a dll that cant be opened instead of an exe that is the main executable. This happens with both Application.ExecutablePath and Assembly.GetExecutingAssembly().Location. How can I solve this and put an exe to the run list to be startup app?

/*
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
 rkApp.SetValue("MyApp", Assembly.GetExecutingAssembly().Location);
                
*/

RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("MMyApp1", Application.ExecutablePath);

Set value is always being with a dll, such as MyApp.dll. Need to be MyApp.exe. Need to solve.

Upvotes: 0

Views: 132

Answers (1)

Hammas
Hammas

Reputation: 1214

Get the execution assembly name dynamically. Check if key already doesn't exist and then add the key.

var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

Assembly curAssembly = Assembly.GetExecutingAssembly();
if (key != null)
{
    var existed = key.GetValue(curAssembly.GetName().Name, curAssembly.Location);
    if (existed == null)
    {
        key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
    }
}       

Upvotes: 1

Related Questions