注册表启动项问题
我在注册表中添加了启动项,程序会开机自动启动。
未能找到文件“C:\Documents and Settings\Administrator\xml_ServerConfig.xml
我的这个XML文件因该是在工作目录的
// 系统配置XML路径
strXmlPath = Directory.GetCurrentDirectory() + @"\xml_ServerConfig.xml"
我在注册表中添加启动项的路径是
Directory.GetCurrentDirectory() + @"\ServerDisplay.exe"
我是这样添加启动项的
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
key.SetValue(exeName, path);//设置为开机启动
我觉得可能是这样。 注册表启动的时候默认的工作目录 是 C:\Documents and Settings\Administrator\
我在程序中取XML路径的时候 strXmlPath = Directory.GetCurrentDirectory() + @"\xml_ServerConfig.xml"
得到的就是 C:\Documents and Settings\Administrator\xml_ServerConfig.xml
可这也有个疑问 我注册表中添加的启动项目录明明是:
D:\ServerInfo\Server\ServerDisplay\bin\Debug\ServerDisplay.exe
为什么它会找到C盘去呢?
我还想请教下。 这个XML配置文件找不到问题该如何处理呢? 不能将它写成绝对路径。
------解决方案--------------------
Directory.GetCurrentDirectory() 并不能保证是程序的工作目录,换成 Application.StartupPath 试试。