程式配置了Config檔案,需要手工的添加 section 節點的資訊,用戶要求用程式實作,請問如何實作,現在沒思路
uj5u.com熱心網友回復:
private void button1_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
TestConfig tc = new TestConfig();
tc.TestStr = "333";
config.Sections.Add("TestConfig", tc);
config.Save(ConfigurationSaveMode.Full);
}
public sealed class TestConfig : ConfigurationSection
{
public static TestConfig GetConfig()
{
TestConfig section = GetConfig("TestConfig");
return section;
}
public static TestConfig GetConfig(string sectionName)
{
TestConfig section = (TestConfig)ConfigurationManager.GetSection(sectionName);
if (section == null)
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
return section;
}
[ConfigurationProperty("TestStr", IsRequired = false)]
public string TestStr
{
get
{
return (string)base["TestStr"];
}
set
{
base["TestStr"] = value;
}
}
}
uj5u.com熱心網友回復:
你這是已經搞定了uj5u.com熱心網友回復:
AppConfig 就是一個xml啊 。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/81399.html
標籤:C#
