【问题标题】:c# - App.config placing and accessingc# - App.config 放置和访问
【发布时间】:2013-01-29 16:18:34
【问题描述】:

我创建了一个使用 App.Config 的 c#.net 应用程序。 当然,调试一切正常。 我可以访问该文件进行读写。

但是当我为我的应用程序创建安装程序时,编写部分不再起作用了。

这是因为应用程序安装在程序文件中。而且您不能写入程序文件文件夹...

我有点想将 app.config 放在 de AppData 文件夹中的解决方案,但我无法让它工作。

我想知道的是如何指示我的安装程序将文件放置在正确的位置,以及如何创建正确的函数将其写入该位置。

这是我目前所拥有的:

 public static void WriteValue(string key, string value)
 {
    string configPath = System.Environment.GetFolderPath(
                                                         Environment.SpecialFolder.ApplicationData) + 
                        "\\Application\\app.config";

    ExeConfigurationFileMap map = new ExeConfigurationFileMap(configPath);

    // Open App.Config of executable
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, 
                                                                           ConfigurationUserLevel.None);

    // Add an Application Setting.
    config.AppSettings.Settings.Remove(key);
    config.AppSettings.Settings.Add(key, value);
    // Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified);
    // Force a reload of a changed section.
    ConfigurationManager.RefreshSection("appSettings");
}

提前谢谢大家

【问题讨论】:

  • 你是如何安装应用程序的?基本思想是在安装过程中赋予文件(即 app.config)适当的访问权限。
  • 应用程序配置文件应保留在安装位置,而不是应用程序数据中。
  • 好像是访问问题,你需要授予app.config文件的写访问权限。
  • 大家好,问题是在 windows vista 和 7 中你不能写入程序文件目录...

标签: c# app-config config


【解决方案1】:

文件App.config是开发过程中使用的名称,在调试过程中VS读取它就好了。

但是当您在工作室外运行您的可执行文件YourProgram.exe 时,它会搜索名为YourProgram.exe.config 的文件

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2020-05-15
    • 2011-08-24
    • 2010-12-23
    相关资源
    最近更新 更多