有关写入app.config文件的问题 [英] A question about writing into app.config file

查看:182
本文介绍了有关写入app.config文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我从这篇文章中获得了一个示例.

这是App.config文件:

I have an example from the article.

here is the App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
    <add key="Setting1" value="Very" />
    <add key="Setting2" value="Easy" />
 </appSettings>
</configuration>



这是控制台程序write.cs:



here is the console program write.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace AppSettings
{
   class Program
   {
     static void ShowConfig()
     {

        // For read access you do not need to call OpenExeConfiguraton
        foreach(string key in ConfigurationManager.AppSettings)
        {
           string value = ConfigurationManager.AppSettings[key];
           Console.WriteLine("Key: {0}, Value: {1}", key, value);
        }
     }

     static void Main(string[] args)
     {

        ShowConfig();

        // Open App.Config of executable
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (ConfigurationUserLevel.None);

        // Add an Application Setting.
        config.AppSettings.Settings.Add("ModificationDate",
                       DateTime.Now.ToLongTimeString() + " ");

        // Save the changes in App.config file.
        config.Save(ConfigurationSaveMode.Modified);

        // Force a reload of a changed section.
        ConfigurationManager.RefreshSection("appSettings");
        ShowConfig();
      }
   }
}



我尝试过的事情:

我期望此ModificationDate将出现在App.config中,但事实证明是在write.exe.config文件中.为什么会这样?



What I have tried:

I am expecting this ModificationDate shall come up in App.config, but it turns out in write.exe.config file. why did this happen? the new variable can not be added into App.Config directly?

推荐答案

如注释中所述,Write.exe.config实际上就是您的app.config文件.它采用可执行文件的名称.
As mentioned in the comments, Write.exe.config actually IS your app.config file. It takes the name of the executable.


这篇关于有关写入app.config文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆