我网站有一个目录Main里面有一个index.html然后根目录也有一个index.html
我想用代码修改Main里面的index.html然后用这个文件替换根目录的index.html着急着急!!!!

解决方案 »

  1.   

    你可以先把根目录的那个index.html文件删除掉,然后将Main里面的index.html上传到根目录里,然后在刷新
      

  2.   


        ZipOutputStream zos = null;
        String strBaseDir = "";
        protected void Button3_Click(object sender, EventArgs e)
        {
            dlZipDir(Server.MapPath("test"), "解压到网站根目录下");
        }
        private void dlZipDir(string strPath, string strFileName)
        {
            MemoryStream ms = null;
            Response.ContentType = "application/octet-stream";
            strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
            Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
            ms = new MemoryStream();
            zos = new ZipOutputStream(ms);
            strBaseDir = strPath + "\\";
            addZipEntry(strBaseDir);
            zos.Finish();
            zos.Close();
            Response.Clear();
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }
        private void addZipEntry(string PathStr)
        {
            byte[] buffer;
            string strEntryName;
            string WebUrl = Request.Url.DnsSafeHost;
            WebUrl = WebUrl.Substring(WebUrl.IndexOf(".", 0) + 1);
            ZipEntry entry;
            DirectoryInfo di = new DirectoryInfo(PathStr);
            foreach (DirectoryInfo item in di.GetDirectories())
            {
                addZipEntry(item.FullName);
            }
            foreach (FileInfo item in di.GetFiles())
            {
                if (item.ToString() == "index1.htm" || item.ToString() == "258a.htm")
                {
                    StreamReader sr = new StreamReader(item.FullName, Encoding.GetEncoding("gb2312"));
                    StringBuilder line = new StringBuilder(sr.ReadToEnd());
                    sr.Close();
                    line.Replace("tihuan", "<a>qqqqqqqqqqqqqqqqqqqqqq</a>");
                    //line.Replace("domainsite", "." + WebUrl);
                    buffer = Encoding.GetEncoding("gb2312").GetBytes(line.ToString());
                    strEntryName = item.FullName.Replace(strBaseDir, "");
                    entry = new ZipEntry("../" + strEntryName);
                    zos.PutNextEntry(entry);
                    zos.Write(buffer, 0, buffer.Length);
                }
            }
            zos.Dispose();
            zos.Close();
        }
    }
    这是下载代码 我看不明白 。 就是把这文件改完了然后保存下载   我不想下载 我想让他替换根目录的index.html 着急
      

  3.   

    我想 一点按钮的时候  就让Main里面的index.html替换 根目录下的index.html  怎么做  
      

  4.   

    你弄个上传页面.. 
    保存路径就是根目录..
    直接把改好的文件覆盖下index.html不就完了?
      

  5.   

    我回复的时候还没第二条啊. 好吧我重发File.Copy(Server.MapPath("被复制的文件相对路径"), Server.MapPath("目的位置相对路径"), true);   第三个参数为true表示目标位置存在同名文件将被覆盖. 
      

  6.   

    不是这意思   我是想通过代码把Main里面index.html文件 替换里面的一部分 然后把这个文件替换根目录下的index.html   而Main里面的index.html不变   还是未改之前的实际我最终目的  就是管理根目录下的index.html    每次里面换内容了  我只需要操作Main里面的index.html文件 把操作完成的文件 替换到根目录下
      

  7.   

    我给别人做网站  然后html页有联系方式 都是静态的 (像这样的静态页有很多)  如果联系方式  什么的变了
    我想给客户一个小的管理页面   让他可以随时更改他的联系方式等。 总不能让客户打打开html页更改啊
      

  8.   

    也就是说Main里面那个就是操作模板了. 就是长期保持不变的对吧..那你就弄个textbox把main/index.html的内容读取出来..直接在textbox里改动.最后点击的时候.. 把文本内容保存到根目录.文件名为index.html就完事啊
      

  9.   

    我怎么样才能把我读出来的代码  替换到   根目录下index.html文件 
    具体代码应该怎么写???  菜鸟
      

  10.   

    需要用代码实现哦,因为你要改变main目录里的内容去替换要目录里的index
    多调试几次看看
      

  11.   

    我想了一下. 你不是有个固定模板main\index.html么. 第一步: 把main\Index.html里.需要改动的值在源文件中用变量代替
        例如: <td>楼主</td> 改成 <td>@$8848_Name$@</td>  
       总之就是极低可能会和其他文本重复的字符串第二步:制作添加页面.就跟注册一样.. 
       读取main\index.html的内容,然后字符串替换. 一个一个的把用户输入的内容替换到对应的约定变量里
        
    第三步:把替换好的字符串保存为根目录下的index.html覆盖
      
      

  12.   

    StreamReader sr = new StreamReader(item.FullName, Encoding.GetEncoding("gb2312"));
    StringBuilder line = new StringBuilder(sr.ReadToEnd());
    sr.Close();
    line.Replace("tihuan", "<a>qqqqqqqqqqqqqqqqqqqqqq</a>");
    //替换部分==================================================================
    StreamWriter sw=new StreamWriter("C:web\\index.html",false,Encoding.GetEncoding("gb2312"));
    sw.Write(line.ToString());
    sw.Flush();
    sw.Close();