日期:2014-05-18  浏览次数:20791 次

C#设置开机启动项的问题
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.SetValue("test", Application.StartupPath + "WindowsFormsApplication1.exe");
runKey.Close();

我用的语句是这样的。现在问题是:
我在Form1_Load事件中去读取一个xml文件,开机启动时就报错误:不能在C:\...\..(这个路径忘记了,就是平时的系统路径)找到test.xml文件。有哪位大侠遇见过吗?教教我

------解决方案--------------------
这个要看你的代码里是怎么取路径的。猜想是“当前路径”取的有问题,如果用
Environment.CurrentDirectory
有可能会取到windows\system32
建议这样取:
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file://", "").Replace("file:\\","");