2012-11-08 40 views
4

我正在使用.NET 4.0,我想使用app.config文件来存储相同的参数设置。我做了以下。我使用项目属性中的Settings选项卡来创建我的参数。C#applicationSettings:如何更新app.config?

这增加了信息的app.config文件是这样的:

<MyApp.Properties.Settings> 
     <setting name="param1" serializeAs="String"> 
     <value>True</value> 
     </setting> 
<MyApp.Properties.Settings> 

在我的视图模型(在我的代码),我可以访问的信息是这样的:

bool myBool = MyApp.Properties.Default.param1; 

当我尝试更改配置文件中的值,我试试这个:

Properties.Settings.Default.param1 = false; 

但是这个cau出现错误,那param1是只读的。

那么如何从我的代码更新我的配置文件?

+1

可能是试试这个:http://stackoverflow.com/questions/1357240/change-the-value-in-app-config-file-dynamicallay –

+0

在这个解决方案,他们使用config.AppSettings。 AppSettings不被弃用?我想使用ApplicationSettings,因为这使用了输入参数。 –

+0

编辑我的答案与EDIT2给出一个链接到一个链接到一个代码,这可能是你的解决方案:) –

回答

3

嗯,我读了Hari Gillala的链接,其中一位用户建议直接编辑app.config文件,这是一个xml文件。

所以在项目属性 - >设置我创建我需要的参数。然后,加载代码的参数我做了以下内容:

_myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1; 

这样一来,我可以很容易地读取配置参数的信息。是打字,所以在设计时间,我可以看到是否正确或不正确。

要更新de config文件,我使用.NET的xml库编辑app.config文件。

System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); 
    xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 
    System.Xml.XmlNode node; 
    node = xml.SelectSingleNode("configuration/applicationSettings/MyApp.Properties.Settings/setting[@name='myparam1']"); 
    node.ChildNodes[0].InnerText = myNewValue; 
    xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

这样,我创建了一个xml文档(xml变量)并加载app.config文件的信息。然后,我搜索要更新的节点,更新其信息(InnerText属性),最后保存更改。

通过这种方式,我可以更新app.config。这是我想要的,因为在便携式应用程序中,只有一个用户会使用它,并且我希望配置应用于运行应用程序的任何计算机中。

2

您应该使用Properties.Settings执行此操作。 有关更多信息,您可以查看此documentation。在评论和进一步的搜索协议之后

//modify 
Properties.Settings.Default.param1 = false; 
//save the setting 
Properties.Settings.Default.Save(); 

编辑:

我的建议,以达到预期的结果将是切换到的AppSettings。 这是因为,经过一些搜索后,我发现appsettings更改了Application Data文件夹中的.config文件(在我的机器上运行一些测试证实了这一点)。

请看this question的回答评论。

我不知道是否有周围的一些工作,但如果你想你的应用程序有一个便携式app.config文件,我认为唯一的办法就是切换到的AppSettings,我相信可以保存更改在程序文件夹中找到的app.config中。

编辑2:可能的解决方法

我发现了一个可能的解决方案,使您的应用程序移植! 您可以更改“设置”使用的提供程序以保存创建自定义提供程序的应用程序设置。

this question的回答提供了一个指向应用程序设置便携式的代码的链接。我想你试试吧

+1

嗯,真的,我用这种方式,Properties.Settings.Default,但我写错了我的代码。这样就是我如何得到错误。 –

+0

我认为您的设置范围是应用程序,您必须将其更改为用户以便能够在运行时对其进行修改 –

+0

在app.config中,您可以将其设置从移动... < applicationSettings>部分添加到部分,或者使用GUI,在设置配置 –

9

这里是我的函数来更新或添加一个条目到app.config中的“applicationSettings”部分。可能有更好的办法,但这对我很有用。如果任何人都可以提出更好的方法,请分享,我们会一直在寻找更好的东西。

static string APPNODE = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings"; 
    static DateTime now = DateTime.Now; 
    Utilities.UpdateConfig(APPNODE, "lastQueryTime", now.ToString()); 

    static public void UpdateConfig(string section, string key, string value) 
    { 
     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

     ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections[section]; 
     SettingElement element = applicationSettingsSection.Settings.Get(key); 

     if (null != element) 
     { 
      applicationSettingsSection.Settings.Remove(element); 
      element.Value.ValueXml.InnerXml = value; 
      applicationSettingsSection.Settings.Add(element); 
     } 
     else 
     { 
      element = new SettingElement(key, SettingsSerializeAs.String); 
      element.Value = new SettingValueElement(); 
      System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
      element.Value.ValueXml = doc.CreateElement("value"); 

      element.Value.ValueXml.InnerXml = value; 
      applicationSettingsSection.Settings.Add(element); 
     } 

     config.Save(ConfigurationSaveMode.Modified); 
     ConfigurationManager.RefreshSection("applicationSettings");    
    } 
相关问题