请问能不能在安装项目的期间根据用户的输入来改写web.config文件的内容?比如说:数据库连接字符串……谢谢!

luoxiang2000 2004-09-27 04:58:00
每次要安装完以后手动去改很麻烦的!
...全文
287 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengsb 2004-09-28
  • 打赏
  • 举报
回复
...
CustomActionData=/Virdir="[TARGETVDIR]" /Port=[PORT]
...
public override void Install(IDictionary stateSaver)
{
try
{
// Uses reflection to find the location of the config file.
System.Reflection.Assembly Asm=System.Reflection.Assembly.GetExecutingAssembly();
string strConfigLoc=Asm.Location;
string strTemp=strConfigLoc;
// Gets the parameter passed across in the CustomActionData.
string Vdir=Context.Parameters["VirDir"];//虚拟目录

strTemp =strTemp.Remove(strTemp.LastIndexOf("\\"),strTemp.Length-strTemp.LastIndexOf("\\"));
strTemp = strTemp.Remove(strTemp.LastIndexOf("\\"),strTemp.Length-strTemp.LastIndexOf("\\"));//物理目录

....
}
finally
{

}
}
速马 2004-09-28
  • 打赏
  • 举报
回复
// 我就是怀疑我的代码没有执行,因为我用如下方法也不起作用
让安装程序走到最后再试(前提是你严格按照李洪根的那个文章配置了安装项目)

// 只能填写一个虚拟路径,比如:test,那么也就是说TARGETDIR=“test”
wrong
IIS标识符为1的站点的主目录路径 + "test" == TARGETDIR
luoxiang2000 2004-09-28
  • 打赏
  • 举报
回复
谢谢,可是我就是怀疑我的代码没有执行,因为我用如下方法也不起作用:
StreamWriter reader = new StreamWriter("c:\\test.txt");
reader.WriteLine(DateTime.Now.ToShortDateString());
reader.Close();

另外,我制作的是web的安装项目呀,不能选择路径吧,只能填写一个虚拟路径,比如:test,那么也就是说TARGETDIR=“test”,这样相当于这样:doc.Load("test" + "\\web.config");
这样不行吧?
速马 2004-09-28
  • 打赏
  • 举报
回复
好好的看看李洪根的那个例子,再来问
速马 2004-09-28
  • 打赏
  • 举报
回复
1、不需要,[TARGETDIR]是用户选了哪个路径就是哪个路径
2、这是windows程序,Console.Write("is running……?");无用。并且你的自定义代码会在所有文件已经复制完毕后才执行,相当于在最后一步执行
luoxiang2000 2004-09-28
  • 打赏
  • 举报
回复
疑问:
1、这个TARGETDIR是要用户在安装的时候填写的,那么他岂不是要填写如:c:/Inetpub/wwwroot/之类的路径才行;
2、我都怀疑我的以下代码有没有运行,又不能调试???有调试的方法吗?
我是这样做的Console.Write("is running……?");,可是都没有反应?

public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
Console.Write("is running……?");
string dir=this.Context.Parameters["targetdir"];

XmlDocument doc = new XmlDocument();
XmlNode inode;
doc.Load(dir + "\\web.config");

inode = doc.DocumentElement.ChildNodes[1].ChildNodes[0];
inode.Value="ttt";
}
vikey 2004-09-28
  • 打赏
  • 举报
回复
web.config
<add key="StrSqlConn" value="^SqlConnectionString^"></add>


public static void UpdateWebConfigForSqlProvider (string connectionString)
{
string webConfigFilePath = Directory.GetCurrentDirectory() + "/web/web.config";
string connectionToken = "^SqlConnectionString^";
string webConfig;

// Read the web.config file
//
StreamReader inputFile = new StreamReader(webConfigFilePath);
webConfig = inputFile.ReadToEnd();
inputFile.Close();

// Find the connection string section
//
int connectionTokenIndex = webConfig.IndexOf(connectionToken);

if (connectionTokenIndex != -1)
{

// Exists, replace value
//
webConfig = webConfig.Replace(connectionToken, connectionString);

}

// All done, write the file back out
//
StreamWriter output = new StreamWriter(File.Open(webConfigFilePath, FileMode.Create));
output.Write(webConfig);
output.Flush();
output.Close();

}


速马 2004-09-28
  • 打赏
  • 举报
回复
/dbname=[CUSTOMTEXTA1] /server=[CUSTOMTEXTA2] /user=[CUSTOMTEXTA3] /pwd=[CUSTOMTEXTA4] /targetdir="[TARGETDIR]\"

这句写对没有?
还有
this.Context.Parameters["targetdir"] -> this.Context.Parameters["TARGETDIR"]

大小写牢记要区分
你确定你操作XML的方式是正确的?
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode node = doc["configuration"]["appSettings"].SelectSingleNode("add[@key=\"dsn\"]");
XmlAttribute attrValue = doc.CreateAttribute("value");
attrValue.Value = "dsnString";
node.Attributes.Append(attrValue);
doc.Save(path);
luoxiang2000 2004-09-28
  • 打赏
  • 举报
回复
我直接将targetdir换成我的虚拟目录test也不行!???
luoxiang2000 2004-09-28
  • 打赏
  • 举报
回复
我是这样写的,可是不起作用:
string dir=this.Context.Parameters["targetdir"];

XmlDocument doc = new XmlDocument();
XmlNode inode;
doc.Load(dir + "\\web.config");

inode = doc.DocumentElement.ChildNodes[1].ChildNodes[0];
inode.Value="ttt";
速马 2004-09-28
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkWalkthroughUsingCustomActionToCreateDatabaseDuringInstallation.asp

http://dev.csdn.net/develop/article/21/article/25/article/27/27814.shtm
速马 2004-09-28
  • 打赏
  • 举报
回复
/targetdir=\"[TARGETDIR]\"
luoxiang2000 2004-09-28
  • 打赏
  • 举报
回复
可是我无法得到 web 部署项目中的虚拟目录的路径 亚!?
gbbword 2004-09-27
  • 打赏
  • 举报
回复
楼上的解决了,我就等着接分了。
顺便顶一下下!
好贴!
速马 2004-09-27
  • 打赏
  • 举报
回复
不知道你仔细看了没有
happyjun2000 2004-09-27
  • 打赏
  • 举报
回复
string dir=this.Context.Parameters["dir"]; //web 部署项目中的虚拟目录的路径。
然后按照写xml的方法
luoxiang2000 2004-09-27
  • 打赏
  • 举报
回复
用System.IO.Directory.GetCurrentDirectory()的话,那么不是我的安装文件必须要拷贝iis根目录下面才能改写成功呀!?
luoxiang2000 2004-09-27
  • 打赏
  • 举报
回复
如果按照写xml的方法,那么路径不好取得!?
winxieddd 2004-09-27
  • 打赏
  • 举报
回复
当然可以呀,你用System.IO.Directory.GetCurrentDirectory()取得当然的工作目录。然后打开那个配置文件,把内容写进去。
luoxiang2000 2004-09-27
  • 打赏
  • 举报
回复
大哥,我是要在安装的时候改web.config文件诶!多谢!呵呵!
加载更多回复(4)

110,536

社区成员

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

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

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