【问题标题】:Modify AppSettings in the App.Config of different Exe修改不同Exe的App.Config中的AppSettings
【发布时间】:2009-06-10 19:20:29
【问题描述】:

我有一个 C# .NET 控制台应用程序 exe,其中 app.config 指定了一些用作参数的 ApplicationSettings。

我还有一个单独的(Windows 窗体)exe(位于同一目录中),以允许用户修改第一个 exe 使用的 ApplicationSettings。

从第二个 exe 修改第一个 exe 的 app.config 最干净的方法是什么?

谢谢。

【问题讨论】:

    标签: c# .net app-config


    【解决方案1】:

    你可以使用

    public static Configuration OpenExeConfiguration(
        string exePath
    )
    

    MSDN Link

    【讨论】:

    • 谢谢。这是否允许我编辑 app.config 中的 applicationSettings,如果可以,如何编辑?我认为 applicationSettings 被认为是“只读的”..
    • 是的,cfg.AppSettings 应该可以工作并允许您更改设置。试试看,我现在不行。
    • 不,据我所知,没有只读,只需使用 GetSection 函数来提取和更改您想要的部分,请参阅给定链接中的示例...
    • 谢谢,效果很好。我对 App.Config 文件的“AppSettings”和“ApplicationSettings”部分感到困惑。
    【解决方案2】:

    用途:

    Configuration cfg = ConfigurationManager.OpenExeConfiguration(path_to_exe_file_of_second_app);
    // do whatever you need with that configuration
    cfg.Save();
    

    请注意,OpenExeConfiguration 方法采用第二个应用程序 exe 文件的路径,而不是配置文件本身。

    【讨论】:

      猜你喜欢
      • 2015-06-05
      • 2018-11-09
      • 1970-01-01
      • 2015-04-03
      • 2013-06-02
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      相关资源
      最近更新 更多