【问题标题】:Cannot load config files when running the program from another program从另一个程序运行程序时无法加载配置文件
【发布时间】: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";

因此,当应用程序运行时,它将首先使用FileStreamXMLSerializer 加载配置文件,如下所示:

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();

当我发现我的应用程序这次无法加载配置文件时,我感到很惊讶。序列化程序失败,表明文件不存在。但是如果我不是从观察者运行程序,而是直接运行应用程序,则完全没有问题。

【问题讨论】:

    标签: c# file process


    【解决方案1】:

    尝试替换:

    private string configFoldername = "Config";
    

    与:

    private string configFoldername = Path.Combine(Application.StartupPath, "Config");
    

    或者设置process.StartInfo.WorkingDirectory:

    process.StartInfo.WorkingDirectory = @"C:\App\bin\Release\";
    

    【讨论】:

    • 啊,是的,问题出在我发现的路径上。不过谢谢你的回答。赞成。这变得特别成问题,因为观察程序实际上没有关于实际 StartUpPath 的变量信息因此,在 WinForm 应用程序中进行了更改。
    【解决方案2】:

    问题在于FileStream 调用:

    filestream = new FileStream(Path.Combine(configFoldername, "MyConfig1.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
    

    给出的路径是使用相对路径,只给出configFoldername而不是完整路径。

    因此,令人惊讶的是,当 WinForm 程序从另一个程序(Watcher)运行时,FileStream 默认路径将是 Watcher 的应用程序路径,而不是WinForm 的应用路径,导致FileStream 无法从正确的文件夹中获取文件。

    为了解决这个问题,我使用绝对路径而不是相对路径。在WinForm应用程序的初始化中,我使用Application.StartupPath来获取WinForm应用程序的绝对路径。

    private string configFoldername = "Config";
    private string configFolderpath; //declare new variable here
    
    ...and in the initialization
    
    string root = Application.StartupPath;
    configFolderpath = Path.Combine(root, configFoldername); //making this having the absolute path.
    

    那么新的反序列化是这样的:

    filestream = new FileStream(Path.Combine(configFolderpath, "MyConfig1.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
    serializer = new XmlSerializer(typeof(MyAppConfig1));
    
    //... some others
    
    filestream = new FileStream(Path.Combine(configFolderpath, "MyConfig2.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
    serializer = new XmlSerializer(typeof(MyAppConfig2));
    
    //... some others
    
    filestream = new FileStream(Path.Combine(configFolderpath, "MyConfig3.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
    serializer = new XmlSerializer(typeof(MyAppConfig3));
    
    //... some others
    

    现在可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2018-08-23
      • 1970-01-01
      相关资源
      最近更新 更多