5.20软工日报

今日配置web.config

public bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue) 
		{ 
            
			try 
			{ 
				string path=WebConfigDirectory+"\\web.config"; 
				XmlDocument xd=new XmlDocument(); 
				xd.Load(path); 
 
				//如果没有appSetting,则添加 
				if(xd.SelectNodes("//appSettings").Count==0) 
				{ 
					xd.DocumentElement.AppendChild(xd.CreateElement("appSettings")); 
				} 
 
				//判断节点是否存在,如果存在则修改当前节点 
				bool addNode=true; 
				foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add")) 
				{ 
					if(xn1.Attributes["key"].Value==appSettingsAddkey) 
					{ 
						addNode=false; 
						xn1.Attributes["value"].Value=keyvalue; 
						// xn1.ParentNode.RemoveChild(xn1); 
						break; 
					} 
				} 
 
				//当前节点不存在,则添加新节点 
				if(addNode) 
				{ 
					//创建新节点 
					XmlNode xn2=xd.CreateElement("add"); 
 
					//添加key 
					XmlAttribute xa=xd.CreateAttribute("key"); 
					xa.Value=appSettingsAddkey; 
					xn2.Attributes.Append(xa); 
 
					//添加value 
					xa=xd.CreateAttribute("value"); 
					xa.Value=keyvalue; 
					xn2.Attributes.Append(xa); 
					xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2); 
				} 
				//保存web.config 
				xd.Save(path); 
				return true; 
			} 
			catch 
			{ 
				return false; 
			} 
} 
posted @ 2024-05-20 23:56  笠大  阅读(3)  评论(0编辑  收藏  举报