Reputation: 51
I have a small WPF Application. I'm trying to get it started on Windows startup My C# code as follows:
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("MyApp", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
key.Close();
}
and deleted RegistryKey:
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue("MyApp", false);
key.Close();
}
I've build one executable file (MyApp.exe) and installed on my computer. But the application still does not run when Windows start up. How can I do? how to change Startup impact "not measured" to other on Task Manager?
I'm using Windows 10 x64. Sorry for my English.
Thanks.
Upvotes: 2
Views: 2561
Reputation: 650
There are two ways to add your program as a startup application:
Add this application to a special path in the Windows registry, like this:
private static void CreateApplicationStartup(string appName, string appPath)
{
try
{
const string softwareMicrosoftWindowsCurrentVersionRun =
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
using (var registryKey =
Registry.CurrentUser.OpenSubKey(softwareMicrosoftWindowsCurrentVersionRun, true))
{
if (registryKey == null)
throw new Exception("Startup registry key not found.");
registryKey.SetValue(appName, appPath);
}
}
catch
{
// ignore
}
}
Add a shortcut of your program to the startup folder of Windows, like this:
private static void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation)
{
var shortcutLocation = Path.Combine(shortcutPath, shortcutName + ".lnk");
var shell = new WshShell();
var shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = ApplicationName;
shortcut.IconLocation = PathManager.Icon;
shortcut.TargetPath = targetFileLocation;
shortcut.Save();
}
After implementing one of these approaches, your application will start after booting the Windows OS.
Please ensure that your application does not execute with administrative privileges.
Upvotes: 0
Reputation: 51
For people who have problems like me, I have solved my problem, it is that my application needs to run as administrator so if I code as above and set app.manifest
<requestedExecutionLevel level = "requireAdministrator" UIAccess = "false "/>
it will not run at startup Windows.
To solve this I change my code from CurrentUser to LocalMachine:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("TechTemp", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
key.Close();
}
and
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue("TechTemp", false);
key.Close();
}
The next problem was how to turn off UAC for your application (if UAC is enabled on your computer), you can see here. About Status impact, your application will still run at the window startup even the state is "not measured” on Task Manager. Thanks Rashid Malik and Sami
Update
You can read here to run application on windows startup without adding a registry key, this worked for me.
Upvotes: 3