在 Windows 启动时启动 wpf 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21348289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch wpf Application on Windows startup
提问by Neal
I have developed a WPF application, now I have to launch the application on windows startup.
To do this, I have written the below code. I got the solution from this answer.
It is adding the key in registry but not launching the application.
我开发了一个 WPF 应用程序,现在我必须在 Windows 启动时启动该应用程序。
为此,我编写了以下代码。我从这个答案中得到了解决方案。
它正在注册表中添加密钥但不启动应用程序。
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
string str = Assembly.GetExecutingAssembly().Location;
key.SetValue("Camaleone", str);
采纳答案by SmithMart
When you start an application normally by double clicking, the working directory is normally the path of the exe file. This means if you reference any settings files in your code it can find them.
当您通过双击正常启动应用程序时,工作目录通常是exe文件的路径。这意味着如果您在代码中引用任何设置文件,它可以找到它们。
But, when you add it to the registry to run on startup, the working directory is c:\windows\system32because it is started by windows itself.
但是,当您将其添加到注册表以在启动时运行时,工作目录是c:\windows\system32因为它是由 Windows 本身启动的。
I normally use this:
我通常使用这个:
public static string BaseDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
this means that BaseDiris now the path to the exe.
这意味着BaseDir现在是 exe 的路径。
Whenever I reference any files, eg a settings file I would use:
每当我引用任何文件时,例如我会使用的设置文件:
string mySettingsFile = Path.Combine(BaseDir, "MySettingsFile.xml");
回答by Rashid Malik
Please check here for details. as you can add entry in registry key for all users or current user.
请在此处查看详细信息。因为您可以为所有用户或当前用户在注册表项中添加条目。

