加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ConfigurationManager.AppSettings不能用于以编程方式创建

发布时间:2020-12-15 22:24:19 所属栏目:百科 来源:网络整理
导读:我遇到了一些有趣的事情.我正在尝试为我们的客户制作一个快速控制台应用程序来运行以更改某些程序上的某些连接字符串.这对客户来说是一个“快速应用程序”,我希望它是“白痴证明”. 该应用程序的主要功能之一是重新创建默认配置文件,如果找不到它. if (!File
我遇到了一些有趣的事情.我正在尝试为我们的客户制作一个快速控制台应用程序来运行以更改某些程序上的某些连接字符串.这对客户来说是一个“快速应用程序”,我希望它是“白痴证明”.

该应用程序的主要功能之一是重新创建默认配置文件,如果找不到它.

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","")),"applicationServer"),"sqlServer"),"applicationServerPort"),"customSearchPath"),"")))));


            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行都将停止正常运行.即使我手动更改重新创建的配置文件中的值,它们也始终为每个键返回空值.

这让我觉得有一些基本的方式配置文件与他们的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>

解决方法

2件事会解决你的问题.

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

ConfigurationManager.RefreshSection( “的appSettings”)

有关配置管理器的信息,请访问MSDN:
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读