修改App.Config 文件问题

jackhello 2007-12-22 12:44:52
怎么去修改啊? 怎么修改失败。。。
...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackhello 2007-12-22
  • 打赏
  • 举报
回复
neng 给点操作XML de 例子吗?
呵呵,谢啦
三省Ta叔 2007-12-22
  • 打赏
  • 举报
回复
其实就是跟操作XML文件差不多。
三省Ta叔 2007-12-22
  • 打赏
  • 举报
回复
protected string GetValue(string AppKey)
{
try
{
string AppKeyValue;
AppKeyValue = (string)System.Configuration.ConfigurationManager.AppSettings[AppKey];
return AppKeyValue;
}
catch (Exception ex)
{
return "";
}
}

protected bool SetValue(string AppKey, string AppValue)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "/" + AppConfigName);
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(AppDomain.CurrentDomain.BaseDirectory + "/" + AppConfigName);
}
catch
{
return false;
}
return true;
}

试试看

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧