紧急求助:有关VS2005制作Web安装程序并修改web.config文件中的连接字符串问题!

shakil 2007-08-26 05:35:38
小弟用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”都是一样的结果;

哪位高手有经验的指导一下小弟吧,赶着提交项目,心里着急啊,谢谢了!
...全文
450 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shakil 2007-08-30
  • 打赏
  • 举报
回复
该问题已经解决了

Node.Attributes.GetNamedItem("connectionStrin").Value = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", ServerName, DBName, AdminName, AdminPwd);

还是感谢大家的帮助
shakil 2007-08-26
  • 打赏
  • 举报
回复
我刚才试了下:
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.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(FileInfo.FullName);
ConnectionStringsSection csSection = config.ConnectionStrings;

for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
{
ConnectionStringSettings cs = csSection.ConnectionStrings[i];

string infor = String.Format("{0},{1}", cs.Name, cs.ConnectionString);

}

但是有个问题, string infor读出来的是我当前机器所装的Framework2.0的machine.config配置文件中的连接字符串,狂晕。。。。。
要怎么才能读到安装文件夹中的那个Web.config中的呢?
FileInfo.FullName中已经设置了安装文件夹中Web.config的路径啊,为什么?请教高手!
我在地球 2007-08-26
  • 打赏
  • 举报
回复
学习
shakil 2007-08-26
  • 打赏
  • 举报
回复
还有一点,我的网站是预编译后的,我将预编译后生成的一些文件、文件夹添加到一个安装和部署类中,详细说明一下吧:
如:
1、预编译后的程序:
文件夹:bin、images
网页文件:home.aspx、a.aspx、b.aspx、....
配置文件:web.config

2、安装程序类
MyInstaller.cs

3、安装和部署项目:Setup
内容包括:bin
images
home.aspx
a.aspx
b.aspx
web.config
内容文件来自MyInstaller(活动)
主输出来自MyInstaller(活动)

在安装程序类中进行相关安装配置代码的编写
这种情况下,System.Web.Configuration.WebConfigurationManager.ConnectionStrings返回的是上面的web.config文件中的连接字符串吗?
shakil 2007-08-26
  • 打赏
  • 举报
回复
我的安装程序用的是window下的类模版->安装程序类,可以在其中使用System.Web.Configuration.WebConfigurationManager类吗?
我先试试!有问题再请教!
Jinglecat 2007-08-26
  • 打赏
  • 举报
回复
请考虑使用,System.Web.Configuration.WebConfigurationManager 类访问 web.config 配置文件,
Jinglecat 2007-08-26
  • 打赏
  • 举报
回复
先判断是否不存在,不存在就创建之

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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