Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ConfigurationManager.AppSettings不适用于以编程方式创建的App.config文件_C#_Xml - Fatal编程技术网

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

C# ConfigurationManager.AppSettings不适用于以编程方式创建的App.config文件,c#,xml,C#,Xml,我遇到了一些有趣的事情。我正在尝试为我们的客户制作一个快速的控制台应用程序来运行,以更改他们的一些程序上的一些连接字符串。这是一个“快速应用程序”为客户,我希望它是“白痴证明” 该应用程序所做的主要事情之一是在找不到默认配置文件时重新创建该文件 if (!File.Exists("./App.config")) { Console.WriteLine("Config file is missing. Creating a n

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

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

 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
行都会停止正常工作。即使我手动更改重新创建的配置文件中的值,它们也总是为每个键返回空值

这让我想到,配置文件与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);
        }  
\u targetDatabaseName=ConfigurationManager.AppSettings[“targetDatabaseName”];
_applicationServer=ConfigurationManager.AppSettings[“applicationServer”];
_sqlServer=ConfigurationManager.AppSettings[“sqlServer”];
_applicationServerPort=ConfigurationManager.AppSettings[“applicationServerPort”];
var emptyKeys=新列表();
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”);
如果(emptyKeys.Count>0)
{
Console.WriteLine(“您缺少以下所需键之一”
+string.Join(“,”,emptyKeys)+“。按”
+“退出应用程序的密钥。”);
Console.ReadKey();
环境。退出(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件事可以解决你的问题

  • 配置文件必须是程序集的名称
  • 您需要告诉configuration manager从文件中重新加载所需的节,而不是从内存中重新加载:

    ConfigurationManager.RefreshSection(“应用设置”)

  • 有关configuration manager的信息可在MSDN中找到:

    如果删除app.config,请运行应用程序(它将保存一个新的app.config),关闭应用程序,然后再次打开应用程序,是否有效?或者,它只是在没有app.config的情况下第一次运行失败吗?不,它仍然会继续为所有内容返回空值。我将发布我所拥有的检查值的代码。给我一秒钟对不起,顺便说一下。。。我已经使用.NET一段时间了,从来没有遇到过用户删除app.config。你确定这对“防白痴”应用程序是必要的吗?我注意到每当我构建应用程序时,我的App.config都会更改为$assemblyname$.config。如果我的项目是名为Program1的控制台应用程序,则配置将重命名为Program1.exe.config-它从不创建app.config。配置管理器查找assemblyname.config此处是运行时更改的文件的MSDN示例:。如果可以的话,我会在早上用它添加一个答案ConfigurationManager.RefreshSection(“appSettings”)是我缺少的,谢谢,现在为我工作了