紧急求助:有关VS2005制作Web安装程序并修改web.config文件中的连接字符串问题!
小弟用ASP.NET 2.0开发了个Web信息发布系统,在制作安装程序时,需要获取用户设置的数据库服务器主机名、用户名、密码,并将该信息写入网站的Web.config配置文件中的<connectionStrings>结点中。
根据前辈的教程和经验,我完成了大部分的工作,如创建数据库,加入存储过程等,但在写入web.config文件时,安装时老是提示“未将对象引用设置到对象的实例”!
其中代码如下:
#region WriteWebConfig 修改web.config的连接数据库的字符串
private bool WriteWebConfig()
{
System.IO.FileInfo FileInfo = new FileInfo(this.Context.Parameters["targetdir"] + "web.config");
if (!FileInfo.Exists)
{
throw new InstallException("Missing config file :" + this.Context.Parameters["targetdir"] + "web.config");
}
System.Xml.XmlDocument xmldocument = new System.Xml.XmlDocument();
xmldocument.Load(FileInfo.FullName);
bool FoundIt = false;
XmlNode root = xmldocument.SelectSingleNode("//connectionStrings");
if (root.HasChildNodes)
{
for (int i = 0; i < root.ChildNodes.Count; i++)
{
XmlNode Node = root.ChildNodes[i];
if (Node.Name == "add")
{
if (Node.Attributes.GetNamedItem("key").Value == "connectionString")
{
Node.Attributes.GetNamedItem("value").Value = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", ServerName, DBName, AdminName, AdminPwd);
FoundIt = true;
}
}
}
if (!FoundIt)
{
throw new InstallException("Error when writing the config file: web.config");
}
xmldocument.Save(FileInfo.FullName);
return FoundIt;
}
#endregion
this.Context.Parameters["targetdir"]是安装后的文件路径:
如:d:\setup1\
我觉得问题应该出在XMLDocumemt对象获取不到或者XMLNode节点获取不到导致属性值为空而报错,但就是不知道哪错了!
System.IO.FileInfo FileInfo = new FileInfo(this.Context.Parameters["targetdir"] + "web.config");中"web.config"不管是改成“/web.config”,还是“\\web.config”都是一样的结果;
哪位高手有经验的指导一下小弟吧,赶着提交项目,心里着急啊,谢谢了!