问题
I currently have problem in making my app autorun during startup in windows 8. I have tried to put the app in the registry. In fact I have tried both Local Machine and Current User approach:
RegistryKey rkHKLM = Registry.LocalMachine;
RegistryKey rkRun;
RegistryKey rkHKCU = Registry.CurrentUser;
RegistryKey rkRun1;
rkRun = rkHKLM.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkRun1 = rkHKCU.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (checkBox1.Checked)
{
rkRun.SetValue("MyApp", Application.ExecutablePath);
rkRun1.SetValue("MyApp", Application.ExecutablePath);
}
else
{
rkRun.DeleteValue("MyApp");
rkRun1.DeleteValue("MyApp");
}
This method does not work in Windows 8. But I don't have this problem in windows 7 or XP. Anything different for Windows 8? Is it there is any new approach?
- For your info, I have set the app to run as admin but still doesn't autorun on startup.
回答1:
Add a link or batch file file to:
c:\Users\ (username) \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
you could probably replace c:\Users\ (username) \AppData\Roaming
with "%AppData%"
in your code by
Environment.GetEnvironmentVariable("AppData");
回答2:
Putting a shortcut in: c:\Users\{login name}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup should run the application when you log in.
Optionally, you could create task. You can in the trigger choose whether it should run on log-in or when the computer starts.
The "Task Scheduler Managed Wrapper" is a helpful library for creating tasks programmatically.
来源:https://stackoverflow.com/questions/30993762/ways-to-autorun-my-c-sharp-app-during-startup-in-windows-8