首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于静态页,过程

2012-12-15 
关于静态页,进程用静态页模板生成静态页 protected void Button1_Click(object sender, EventArgs e){//在

关于静态页,进程
用静态页模板生成静态页


 protected void Button1_Click(object sender, EventArgs e)
    {
        //在生成新文件之前,删除原来的
        System.IO.DirectoryInfo pathd = new System.IO.DirectoryInfo(Server.MapPath("~/html"));//删除html文件夹下文件
        deletefile(pathd);

        StreamWriter sw = null;
        StreamReader sr = null;
        string htmlfilename = "";
        string str1 = "";
        string path1 = HttpContext.Current.Server.MapPath("i.htm");//模板
        string path = HttpContext.Current.Server.MapPath("~/html/");//生成文件放在html文件夹下
        try
        {
            sr = new StreamReader(path1, Encoding.GetEncoding("GB2312"));
            str1 = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sr.Close();
        }
        try
        {
            for (int i = 0; i < 4; i++)
            {

                htmlfilename = ""+ i +".html";
                sw = new StreamWriter(path + htmlfilename, false, Encoding.GetEncoding("GB2312"));
                str1 = str1.Replace("$i$", i.ToString());
                sw.Write(str1);
                sw.Flush();
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
    }


    //删除
    private void deletefile(System.IO.DirectoryInfo path)
    {
        foreach (System.IO.DirectoryInfo d in path.GetDirectories())
        {
            deletefile(d);
        }
        foreach (System.IO.FileInfo f in path.GetFiles())
        {
            f.Delete();
        }
    }


现在的问题是,生成html文件后,在点生成,就出现问题了

如果不用删除,直接覆盖也是同样的问题:0.html”正由另一进程使用,因此该进程无法访问该文件。
如果生成html文件后等一段时间在点生成,就不出现这样的问题了
求解决的办法

[最优解释]
using(StreamWriter sw=new StreamWriter)
{
}
就这种类型
连数据库也有这种方式
[其他解释]
你创建文件的时候没释放掉,你先释放掉吧。重启下电脑或者关闭相关的进程。然后再删除,在下次创建的时候记得释放掉。其实你静态划的时候不一定要删除原来的,你可以把原来里面的内容替换的不更好吗
[其他解释]
引用:
你创建文件的时候没释放掉,你先释放掉吧。重启下电脑或者关闭相关的进程。然后再删除,在下次创建的时候记得释放掉。其实你静态划的时候不一定要删除原来的,你可以把原来里面的内容替换的不更好吗

关掉VS,在重新打开确实就没这个问题了
怎么释放掉啊
sr.Close();
sr.Dispose();
sw.Close();
sw.Dispose();
这样也不行啊
关进程我不知道怎么关啊,该怎么关
新手
[其他解释]
那你的代码测试,完全没问题。重启你的电脑,然后运行你代码试试
[其他解释]
引用:
那你的代码测试,完全没问题。重启你的电脑,然后运行你代码试试

重启电脑,在运行代码,2次就出这个问题
怎么关进程,这个进程名字又叫什么啊
[其他解释]
我所知道的关于引用类型
大多靠微软的GC释放
但是文件资源是不能用GC释放的
用using(){}试试
[其他解释]
引用:
我所知道的关于引用类型
大多靠微软的GC释放
但是文件资源是不能用GC释放的
用using(){}试试

using(){}不知道怎么用啊

热点排行