//配置文件的路径
public static string ConfigPath = Environment.CurrentDirectory + "\\config.xml";
//软件显示列表xml 的路径
public static string SoftListXmlPath = Environment.CurrentDirectory + "\\Software.xml";
但是自启后程序读取这两个xml的路径都变成了C:\Windows\System32
原因:
System.Environment.CurrentDirectory确实是获取当前目录没错,但是如果A程序调用B程序,B程序里使用了System.Environment.CurrentDirectory获取目录。那么这个B程序里获取的这个目录就不再是B的应用程序所在目录了,而变成了A所在的目录了。
这也就不难发现我在开机时弹出的目录是C:\Windows\System32,因为开机自启动程序也是由windows的某个进程调用的。
解决:
winform程序要获取当前应用程序所在目录最好不要用System.Environment.CurrentDirectory。而改为:Application.StartupPath 应用程序启动路径 这样不管谁调用它都没问题。