在启动时启动应用程序,使用错误的路径进行加载 [英] Starting application on start-up, using the wrong path to load
问题描述
我正在使用注册表项将我的应用程序设置为在Windows启动时加载(在用户登录后).我的代码:
I am using registry key to set my application to load on Windows Startup(after a user login). My Code:
RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
RegKey.SetValue("AppName", "\"" + @"C:\Users\Name\Desktop" + "\"");
RegKey.Close();
因此,使用此代码,我的应用程序将在启动时加载,但是工作目录为
So with this code, my application load at startup, however the working directory is
C:\ Windows \ System32
C:\Windows\System32
有人知道为什么吗?
这对我不起作用,因为该程序需要与该目录在同一目录中的几个文件.如果程序加载到我选择的目录("C:\ Users \ Name \ Desktop")上,则该问题将不存在.
This does not work for me because that program needs couple of files within the same directory as that one to operate. If the program loaded on my chosen directory("C:\Users\Name\Desktop") then the problem would not exist.
有人对此有何建议?
推荐答案
Directory.SetCurrentDirectory()
可用于在应用启动时设置工作目录.可以使用 Application.ExecutablePath
来检索EXE路径.
Directory.SetCurrentDirectory()
can be used to set your working directory when the app starts. EXE path can be retrieved using Application.ExecutablePath
.
将它们放在一起:
var fi = new FileInfo(Application.ExecutablePath);
Directory.SetCurrentDirectory(fi.DirectoryName);
这篇关于在启动时启动应用程序,使用错误的路径进行加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!