改写web.config

来源:互联网 发布:淘宝网 腰带 编辑:程序博客网 时间:2024/04/30 00:45

public   override   void   Install(System.Collections.IDictionary   stateSaver)
{
try
{
FileInfo   myFileInfo=new   FileInfo(this.Context.Parameters["targetdir"]+@"/Web.config");

if   (!myFileInfo.Exists)
{    
throw   new   InstallException("No   find   the   web.config");
}

XmlDocument   myXmlDocument=new   XmlDocument();
myXmlDocument.Load(myFileInfo.FullName);

//search   the   appSetting   Node
bool   FoundIt;
FoundIt=false;

foreach   (XmlNode   myNode   in   myXmlDocument["configuration"]["appSettings"])
{
if   (myNode.Name=="add")
{
if   (myNode.Attributes.GetNamedItem("key").Value=="ConnectionString")
{
myNode.Attributes.GetNamedItem("value").Value=String.Format("server={0};database={1};uid={2};pwd={3}",this.Context.Parameters["server"],this.Context.Parameters["dbname"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);
FoundIt=true;
}
}
}

if(FoundIt==false)
{
throw   new   InstallException("web.Config   not   inculding   ConnectionString");
}
myXmlDocument.Save(myFileInfo.FullName);
}
catch(Exception   ex)
{
throw   ex;
}
}

原创粉丝点击