【发布时间】:2016-06-03 05:16:20
【问题描述】:
我创建了一个WinFrom 应用程序,发布.exe 位于某个目录中,比如说,
C:\App\bin\Release\WinFormApp.exe
在应用程序目录中,我有一些配置文件以XML 格式编写并存储在目录的Config 子文件夹中:
C:\App\bin\Release\Config\MyConfig1.xml
C:\App\bin\Release\Config\MyConfig2.xml
C:\App\bin\Release\Config\MyConfig3.xml
然后我在应用程序的.cs 文件中有一个private field,用于存储配置文件所在的默认子目录名称:
private string configFoldername = "Config";
因此,当应用程序运行时,它将首先使用FileStream 和XMLSerializer 加载配置文件,如下所示:
filestream = new FileStream(Path.Combine(configFoldername, "MyConfig1.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
serializer = new XmlSerializer(typeof(MyAppConfig1));
//... some others
filestream = new FileStream(Path.Combine(configFoldername, "MyConfig2.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
serializer = new XmlSerializer(typeof(MyAppConfig2));
//... some others
filestream = new FileStream(Path.Combine(configFoldername, "MyConfig3.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
serializer = new XmlSerializer(typeof(MyAppConfig3));
//... some others
到目前为止一切顺利。应用程序运行没有问题。但随后我为我的应用程序创建了一个简单的watcher 程序来检查它是否运行良好,如果不是,那么观察程序将重新启动程序。我的watcher 程序中的相关部分如下所示:
//In the Watcher program
Process process = new Process();
process.StartInfo.FileName = @"C:\App\bin\Release\WinFormApp.exe";
process.Start();
当我发现我的应用程序这次无法加载配置文件时,我感到很惊讶。序列化程序失败,表明文件不存在。但是如果我不是从观察者运行程序,而是直接运行应用程序,则完全没有问题。
【问题讨论】: