ConfigurationManager.AppSettings不适用于以编程方式创建的App.config文件

本文关键字:创建 App config 文件 方式 编程 AppSettings 不适用 适用于 ConfigurationManager | 更新日期: 2023-09-27 18:27:10

我遇到了一些有趣的事情。我正在尝试为我们的客户制作一个快速控制台应用程序,以更改他们的一些程序上的一些连接字符串。这是一个";快速应用程序";对于客户,我希望它是";白痴证明";。

该应用程序所做的主要事情之一是,如果找不到默认配置文件,则重新创建该文件

 if (!File.Exists("./App.config"))
        {
            Console.WriteLine("Config file is missing. Creating a new one now..");
            //file doesn't exist, let's create one.
            var doc = new XDocument(
                new XDeclaration("1.0", "UTF-16", null)
                ,new XElement("configuration"
                    , new XElement("startup"
                        , new XElement("supportedRuntime", new XAttribute("version", "v4.0")
                            , new XAttribute("sku", ".NETFramework,Version=v4.5")))
                , new XElement("appSettings"
                    , new XElement("add", new XAttribute("key", "targetDatabaseName"), new XAttribute("value", ""))
                    , new XElement("add", new XAttribute("key", "applicationServer"), new XAttribute("value", ""))
                    , new XElement("add", new XAttribute("key", "sqlServer"), new XAttribute("value", ""))
                    , new XElement("add", new XAttribute("key", "applicationServerPort"), new XAttribute("value", ""))
                    , new XElement("add", new XAttribute("key", "customSearchPath"), new XAttribute("value", "")))));

            using (var sw = new StringWriter())
            {
                using (var xw = XmlWriter.Create(sw))
                {
                    doc.Save(xw);
                    xw.Close();
                }
                doc.Save("./App.config");
                sw.Close();
                
            }
        }  

在测试过程中,我注意到,如果原始的App.config文件被删除并使用该应用程序重新创建,那么所有的ConfigurationManager.AppSettings行都将停止正常工作。即使我在重新创建的配置文件中手动更改值,它们也总是为每个键返回null值。

这让我想到有一些基本的方式来匹配配置文件和它们的exe文件?

如何让我的应用程序识别动态生成的app.config文件

编辑

我添加了用于从下面的配置文件中获取值的代码,以及我的应用程序创建的当前配置文件。

 _targetDatabaseName = ConfigurationManager.AppSettings["targetDatabaseName"];
        _applicationServer = ConfigurationManager.AppSettings["applicationServer"];
        _sqlServer = ConfigurationManager.AppSettings["sqlServer"];
        _applicationServerPort = ConfigurationManager.AppSettings["applicationServerPort"];
        var emptyKeys = new List<string>();
        if (string.IsNullOrEmpty(_targetDatabaseName))
            emptyKeys.Add("targetDatabaseName");
        if(string.IsNullOrEmpty(_applicationServer))
            emptyKeys.Add("applicationServer");
        if(string.IsNullOrEmpty(_sqlServer))
            emptyKeys.Add("sqlServer");
        if(string.IsNullOrEmpty(_applicationServerPort))
            emptyKeys.Add("applicationServerPort");
        if (emptyKeys.Count > 0)
        {
            Console.WriteLine("You are missing one of the following required keys "
            + string.Join(",", emptyKeys) +". Press"
                + " a key to exit the application.");
            Console.ReadKey();
            Environment.Exit(0);
        }  

这是当前的配置文件以及实际值

<?xml version="1.0" encoding="utf-16"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="targetDatabaseName" value="" />
    <add key="applicationServer" value="" />
    <add key="sqlServer" value="" />
    <add key="applicationServerPort" value="" />
    <add key="customSearchPath" value="" />
  </appSettings>
</configuration>

ConfigurationManager.AppSettings不适用于以编程方式创建的App.config文件

2件事可以解决您的问题。

  1. 配置文件需要是程序集的名称
  2. 您需要告诉配置管理器从文件中重新加载您想要的部分,而不是内存中的内容:

    ConfigurationManager.RefreshSection("appSettings")

有关配置管理器的信息可以在MSDN中找到:https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx