webConfig是否可以写啊????不是读!!!!!!!!!!!

yuanzhihua520 2005-12-08 11:47:13
webConfig是否可以写啊????不是读!!!!!!!!!!!!!11


如果可以写,能不能给点代码呢??????????????
...全文
205 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
int64 2006-05-25
  • 打赏
  • 举报
回复
shouchangle
singlepine 2005-12-18
  • 打赏
  • 举报
回复
http://singlepine.cnblogs.com/articles/293683.html
singlepine 2005-12-09
  • 打赏
  • 举报
回复
1.建一个class,代码如下

using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Xml;
public enum ConfigFileType
{
WebConfig,
AppConfig
}

namespace WebApplication1
{
/// <summary>
/// Summary description for ReadWriteConfig.
/// </summary>
public class ReadWriteConfig
{
public string docName = String.Empty;
private XmlNode node=null;
private int _configType;
public int ConfigType
{
get{ return _configType; }
set{ _configType=value; }
}

#region SetValue
public bool SetValue(string key, string value)
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new InvalidOperationException( "appSettings section not found");
}
try
{
// XPath select setting "add" element that contains this key
XmlElement addElem= (XmlElement)node.SelectSingleNode("//add[@key='" +key +"']") ;
if(addElem!=null)
{
addElem.SetAttribute("value",value);
}
// not found, so we need to add the element, key and value
else
{
XmlElement entry = cfgDoc.CreateElement("add");
entry.SetAttribute("key",key);
entry.SetAttribute("value",value);
node.AppendChild(entry);
}
//save it
saveConfigDoc(cfgDoc,docName);
return true;
}
catch
{
return false;
}
}

#endregion

#region saveConfigDoc
private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
{
try
{
XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null );
writer.Formatting = Formatting.Indented;
cfgDoc.WriteTo( writer );
writer.Flush();
writer.Close();
return;
}
catch
{
throw;
}
}

#endregion

#region removeElement
public bool removeElement (string elementKey)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new InvalidOperationException( "appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild( node.SelectSingleNode("//add[@key='" +elementKey +"']") );
saveConfigDoc(cfgDoc,docName);
return true;
}
catch
{
return false;
}
}
#endregion

#region modifyElement
public bool modifyElement (string elementKey)
{
try
{
XmlDocument cfgDoc = new XmlDocument();
loadConfigDoc(cfgDoc);
// retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings");
if( node == null )
{
throw new InvalidOperationException( "appSettings section not found");
}
// XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" +elementKey +"']"));
saveConfigDoc(cfgDoc,docName);
return true;
}
catch
{
return false;
}
}
#endregion

#region loadConfigDoc
private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
{
// load the config file
if( Convert.ToInt32(ConfigType)==Convert.ToInt32(ConfigFileType.AppConfig))
{
docName= ((Assembly.GetEntryAssembly()).GetName()).Name;
docName += ".exe.config";
}
else
{
docName=HttpContext.Current.Server.MapPath("web.config");
}
cfgDoc.Load( docName );
return cfgDoc;
}
#endregion
}
}
---------------------------------
2.测试
private void Button2_Click(object sender, System.EventArgs e)
{
//新增
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType = (int)ConfigFileType.WebConfig;
config.SetValue(this.TextBox1.Text,this.TextBox2.Text);
}

private void Button1_Click(object sender, System.EventArgs e)
{
//删除
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType = (int)ConfigFileType.WebConfig;
config.removeElement(this.TextBox1.Text);
}

private void Button3_Click(object sender, System.EventArgs e)
{
//修改
ReadWriteConfig config = new ReadWriteConfig();
config.ConfigType = (int)ConfigFileType.WebConfig;
config.SetValue(this.TextBox1.Text,this.TextBox2.Text);
}

具体看这里
http://singlepine.cnblogs.com/articles/293683.html
shalen520 2005-12-09
  • 打赏
  • 举报
回复
可以写,不过写过之后,应用程序会重启
yuanzhihua520 2005-12-09
  • 打赏
  • 举报
回复
谢谢.
jiang8282 2005-12-09
  • 打赏
  • 举报
回复
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");
}
伴老思源 2005-12-09
  • 打赏
  • 举报
回复
可以写,就像读写XML一样
ybbigepl 2005-12-08
  • 打赏
  • 举报
回复
用File对象试一下。

110,572

社区成员

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

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

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