开发者

how can I run an app automatic after restart?

开发者 https://www.devze.com 2023-04-06 06:46 出处:网络
how can I run an app automatic after restart? (by c# cod开发者_如何学Pythone) I create A new string in \'runOnce\' key in registry with the path of the App.

how can I run an app automatic after restart? (by c# cod开发者_如何学Pythone) I create A new string in 'runOnce' key in registry with the path of the App. the OS run this APP before it load the OS my problem is: My APP loads but explorer doesn't load, after I close my APP, explorer loads I restart the computer in APP, and after restart I want that my APP reopen


When you click restart from your app, make the following modifications to the registry:

Create an entry in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry branch.

Use

Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName");

to create an entry.

And

RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName", true);

myKey.SetValue("YourAppName", "AppExecutablePath", RegistryValueKind.String);

to set the run path.

After the system has restarted, your app starts and removes the restart entry by calling this:

Registry.LocalMachine.DeleteSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName");


It seems like your best bet would be to add your program to RunOnce, instead of Run. That way it will be started after the next reboot, but you won't have to worry about erasing the key afterwards.

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce


This is a better answer as you should not create a SubKey. Also this will automatically dispose.

string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(runKey, true))
{
    key.SetValue("MyProgram", @"C:\MyProgram.exe");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号