我开发了一个 WPF 应用程序,现在我必须在 Windows 启动时启动该应用程序。
为此,我编写了以下代码。我从 this answer 得到了解决方案.
它在注册表中添加 key 但不启动应用程序。
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string str = Assembly.GetExecutingAssembly().Location;
key.SetValue("Camaleone", str);
最佳答案
正常双击启动应用程序时,工作目录一般是exe文件的路径。 这意味着如果您在代码中引用任何设置文件,它就可以找到它们。
但是,当您将它添加到注册表以在启动时运行时,工作目录是 c:\windows\system32
,因为它是由 windows 本身启动的。
我通常使用这个:
public static string BaseDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
这意味着 BaseDir
现在是 exe 的路径。
每当我引用任何文件时,例如我会使用的设置文件:
string mySettingsFile = Path.Combine(BaseDir, "MySettingsFile.xml");
关于c# - 在 Windows 启动时启动 wpf 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21348289/