帮你顶一下,
我遇到类似问题,不过是win Form
用access数据库,但只能写在
Data Souce= "绝对路径"
可这样把应用程序换个目录,就找不到数据库了
VB里可以实现,直接放到同一目录下,
可C#里不知为什么不行

解决方案 »

  1.   

    http://www.showyou.net/mycode/ArticleView/Article_4641.html
      

  2.   

    修改web.config操作等同XML文件操作,你按照那个例子再改改吧
      

  3.   

    http://dev.csdn.net/develop/article/27/27814.shtm
      

  4.   

    大哥,我是要在安装的时候改web.config文件诶!多谢!呵呵!
      

  5.   

    当然可以呀,你用System.IO.Directory.GetCurrentDirectory()取得当然的工作目录。然后打开那个配置文件,把内容写进去。
      

  6.   

    如果按照写xml的方法,那么路径不好取得!?
      

  7.   

    用System.IO.Directory.GetCurrentDirectory()的话,那么不是我的安装文件必须要拷贝iis根目录下面才能改写成功呀!?
      

  8.   

    string dir=this.Context.Parameters["dir"];          //web 部署项目中的虚拟目录的路径。
    然后按照写xml的方法
      

  9.   

    可是我无法得到 web 部署项目中的虚拟目录的路径 亚!?
      

  10.   

    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
      

  11.   

    我是这样写的,可是不起作用:
    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";
      

  12.   

    我直接将targetdir换成我的虚拟目录test也不行!???
      

  13.   

    /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);
      

  14.   

    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();
                
        }
      

  15.   

    疑问:
    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";
    }
      

  16.   

    1、不需要,[TARGETDIR]是用户选了哪个路径就是哪个路径
    2、这是windows程序,Console.Write("is running……?");无用。并且你的自定义代码会在所有文件已经复制完毕后才执行,相当于在最后一步执行
      

  17.   

    谢谢,可是我就是怀疑我的代码没有执行,因为我用如下方法也不起作用:
    StreamWriter reader = new StreamWriter("c:\\test.txt"); 
    reader.WriteLine(DateTime.Now.ToShortDateString());
    reader.Close();另外,我制作的是web的安装项目呀,不能选择路径吧,只能填写一个虚拟路径,比如:test,那么也就是说TARGETDIR=“test”,这样相当于这样:doc.Load("test" + "\\web.config");
    这样不行吧?
      

  18.   

    // 我就是怀疑我的代码没有执行,因为我用如下方法也不起作用
    让安装程序走到最后再试(前提是你严格按照李洪根的那个文章配置了安装项目)// 只能填写一个虚拟路径,比如:test,那么也就是说TARGETDIR=“test”
    wrong
    IIS标识符为1的站点的主目录路径 + "test" == TARGETDIR
      

  19.   

    ...
    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
    {

    }
    }