如题,我知道在项目属性-“设置”中可以添加设置,但是如何在程序中动态添加设置呢?配置文件中对应的是下面这种形式<userSettings>
<Sample.Properties.Settings>
<setting name="HH" serializeAs="String">
<value>ZHIJUN</value>
</setting>
</Sample.Properties.Settings>
</userSettings>
<Sample.Properties.Settings>
<setting name="HH" serializeAs="String">
<value>ZHIJUN</value>
</setting>
</Sample.Properties.Settings>
</userSettings>
使用XMLDocument
DataSet ds = new DataSet();
ds.ReadXml("c:\\config.xml");
string param1 = ds.Tables["Table"].Rows[0]["param1"].ToString();
http://www.cnblogs.com/wangsu/archive/2008/02/25/1081226.html
而且配置文件要重启才生效,如果你刷新的话有些东西会变的
直接操作XML文件好些,但是XML文件所属文件夹一定要把可读属性勾掉,不然会报错的
这只是一个建议,如果你坚持修改AppConfig的话可以使用以下函数://AppKey是修改节点的Key,AppValue是传进去的新值
public static void SetValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
//获取可执行文件的路径和名称
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");//这里可以改成你的节点 xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}修改XML的话是这个,下面这个是我用的,你根据情况改一下路径和节点内容 void Modify(String DelayShowingNum)
{
try
{
System.IO.DirectoryInfo mDir = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath);
System.IO.DirectoryInfo dir = mDir.Parent;
System.IO.DirectoryInfo dir_a = dir.Parent;
string usr = dir_a.FullName + "\\ProvinceConfig\\Provinces.xml";//得到存省份信息的xml文件
XElement xes = XElement.Load(usr);//加载xml文件
IEnumerable<XElement> elements = from e in xes.Elements("DisplayPub") select e;
foreach (XElement xe in elements)
{
if (xe.Name == "DisplayPub")
{
xe.Value = DelayShowingNum;
}
}
xes.Save(usr);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// 获取一个值,改值指示配置文件的AppSettings配置节中是否含有和参数“key”同名的键
/// </summary>
/// <param name="key">要判断的key</param>
/// <returns></returns>
public static bool HasAppSettingsKey(string key)
{
bool hasKey = false; for (int i = 0; i < ConfigurationManager.AppSettings.Keys.Count; i++)
{
if (ConfigurationManager.AppSettings.Keys[i] == key)
{
hasKey = true;
}
} return hasKey;
} /// <summary>
/// 给appSettings节添加元素(键值对)
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="ex">out类型参数:存储错误信息</param>
/// <returns></returns>
public static bool AddAppSettings(string key, string value, out string ex)
{
try
{
if (HasAppSettingsKey(key))
{ Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[key].Value = value;
config.AppSettings.SectionInformation.ForceSave = true;
config.Save();
ConfigurationManager.RefreshSection("appSettings"); ex = "更新成功";
}
else
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add(key, value);
config.AppSettings.SectionInformation.ForceSave = true;
config.Save();
ConfigurationManager.RefreshSection("appSettings");
//config.AppSettings.Settings[key]. ex = "添加成功";
} return true;
}
catch
{
ex = "添加失败";
return false;
}
} /// <summary>
/// 加密appSettings配置节
/// </summary>
/// <param name="ex">out类型参数:存储错误信息</param>
/// <returns></returns>
public static bool EncryptAppSettings(out string ex)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.AppSettings.SectionInformation.ForceSave = true;
config.Save();
ex = "加密成功"; return true;
}
catch (Exception e)
{
ex = e.Message;
return false;
}
} /// <summary>
/// 加密connectionStrings配置节
/// </summary>
/// <param name="ex">out类型参数:存储错误信息</param>
/// <returns></returns>
public static bool EncryptConnectionStrings(out string ex)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.ConnectionStrings.SectionInformation.ForceSave = true;
config.Save();
ex = "加密成功"; return true;
}
catch(Exception e)
{
ex = e.Message;
return false;
}
}