ConfigurationManager使用

时间:2015-06-24 00:44:20

标签: c# configurationmanager

我编写了以下代码来获取ConfigurationManager数据,以便更新它:

private static void UpdateConfigurationFile()
{
#if DEBUG
    string applicationName =
        Environment.GetCommandLineArgs()[0];
#else 
   string applicationName =
  Environment.GetCommandLineArgs()[0]+ ".exe";
#endif

    string exePath = System.IO.Path.Combine(
        Environment.CurrentDirectory, applicationName);

    // Get the configuration file. The file name has 
    // this format appname.exe.config.
    System.Configuration.Configuration config =
      ConfigurationManager.OpenExeConfiguration(exePath);

    string server = ConfigurationManager.AppSettings["server"];
}

在代码的末尾,服务器为空。

XML具有相应的服务器代码:

<applicationSettings>
    <forLiDAR.Properties.Settings>
        <setting name="Server" serializeAs="String">
            <value>localhost</value>
        </setting>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

好的,我有解决问题的方法。

不容易解释,因为这不是你的错, VS有一个被它拖了多年的bug。

让我们一步一步来看:

  1. 使用右键单击项目并转到设置选项卡添加设置 - 要遵循的错误路径..这就是原因:
  2. 如果我们遵循documentation,我们所要做的就是:

    Properties.Settings.Default.Server = "Now I'm something different than localhost";
    Properties.Settings.Default.Save();
    

    不需要OpenExeConfigration或其他任何东西,只需要那两行!和像魅力一样的.NET应该能够将新设置保存到配置文件中..除了它不要:) 相反,它喊叫&#39;配置系统无法初始化&#39; !!

    所以..我们认为&#34;好吧,他们并没有在文档中涵盖所有内容..让我们添加我们认为缺少的东西。&#34;所以我们补充说:

    var exePath = System.IO.Path.Combine(Environment.CurrentDirectory, "ConsoleApplication58.vshost.exe.config");
    var config = ConfigurationManager.OpenExeConfiguration(exePath);
    var oldSetting = config.AppSettings.Settings["Server"].Value;
    

    我们将得到......另一个例外 - 这次是null引用。 所以我们问 - &#34;那里发生了什么?!&#34;从调试中我们为&#39; config.AppSettings.Settings&#39;添加了监视功能。令我们惊恐的是,我们看到.NET实际上试图告诉我们&#34;没有勺子......&#34; (或设置计数为0)

    所以在这一点上,我们可以得出结论,这个垃圾绝对不适用于使用AppConfig的应用程序..但它可能适用于使用WebConfig的应用程序 - 我没有测试它。 (哦,只是为了说清楚:&#39;这个废话&#39;表示通过右键单击项目菜单并转到设置选项卡来添加设置文件)

    1. 好的......让我们尝试另一种方法,即添加新项目菜单中的添加设置文件,其默认文件名将是&#39; settings1.settings&#39;它将保留在常规项目文件夹名称下,而不是以前的属性。
    2. 所以我也这样做并测试了以下几行:

      var oldSetting = Settings1.Default.Server;
      Settings1.Default.Server = "localhost2";
      Settings1.Default.Save();
      

      这很有效 - 但我需要告诉你的是&#39;范围&#39;必须是&#39;用户&#39;而不是应用..如果它的应用程序&#39;该设置将没有setter ..它在哪里保存信息?在该用户的AppData文件夹中..这是合乎逻辑的。

      这当然意味着为了在调试保存内容时手动检查,您需要转到AppData配置文件,该文件位于AppData文件夹中,而不是在debug / bin文件夹中。

      1. 但所有这一切当然不是你要求的...... 因为你想要保存一个“应用程序”。范围设置 - 对吗? 嗯..你不能 - 他们不是为此而设计的。 你可以在这里阅读:https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
      2.   

        ApplicationSettings类没有   支持将设置保存到   app.config文件。非常感谢   设计,正确运行的应用程序   安全的用户帐户(想想Vista UAC)   没有写入权限   程序的安装文件夹。

             

        你可以用这个来对抗系统   ConfigurationManager类。但是   琐碎的解决方法是进入   设置设计师并更改   设置用户的范围。如果说   造成困难(比如,设置是   与每个用户相关),你应该   将您的选项功能单独放在一起   程序,所以你可以要求   特权提升提示。或者放弃   使用设置。

        所以,最后我会尝试向您展示您的应用应该如何:

        namespace ConsoleApplication58
        {
            class Program
            {
                static void Main(string[] args)
                {
                    UpdateConfigurationFile();
                }
        
                private static void UpdateConfigurationFile()
                {
                    var oldSetting = Settings1.Default.Server; //to get the current setting if you want..
                    Settings1.Default.Server = "localhost2"; //to change the setting.
                    Settings1.Default.Save(); //to save settings.
                }
            }
        }
        

        如果你按照我的提法添加了Settings1.Settings文件,这就是appConfig的样子:

        <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
        
            <configSections>
                <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                    <section name="ConsoleApplication58.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
                </sectionGroup>
            </configSections>
            <startup> 
                <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
            </startup>
        
            <userSettings>
                <ConsoleApplication58.Settings1>
                    <setting name="Server" serializeAs="String">
                        <value>localhost</value>
                    </setting>
                </ConsoleApplication58.Settings1>
            </userSettings>
        </configuration>
        

        根据评论中的要求,这里是错误报告的链接:
        Configuration in the App.Config and ApplicationSettings