分享

网页静态化,生成静态文件的类

 jsjjtylz 2014-01-11
网页静态化,生成静态文件的类

using System;
using System.IO;
using System.Net;
using System.Web;
namespace ClassLibrary.DataClass
{
/// <summary>
/// GenHtml 的摘要说明。
/// </summary>
public class GenHtml
{
     public static    string path="";
     public static    string fileName="";
     public static    string transUrl="";

    public GenHtml()
    {
     //
     // TODO: 在此处添加构造函数逻辑
     //
    }

    /// <summary>
    /// 转换成静态文件
    /// </summary>
    /// <param name="path">要写入静态文件的真实路径</param>
    /// <param name="fileName">真实的文件名</param>
    /// <param name="transUrl">要转换的地址</param>
    public static bool writeFile(string path, string fileName, string transUrl)
    {

     //是否正确生成
     bool bSuccess = false;
     StreamReader sr;
     StreamWriter sw;
     try 
     {
      WebRequest HttpWebRequest = WebRequest.Create(transUrl);
      WebResponse HttpWebResponse = HttpWebRequest.GetResponse();
      sr = new StreamReader(HttpWebResponse.GetResponseStream());
      string strHtml = sr.ReadToEnd();
      sr.Close();     
      if(!System.IO.Directory.Exists(path))
      {
    
      System.IO.Directory.CreateDirectory(path);
      }
      string PathAndName = path + fileName;
      if (System.IO.File.Exists(PathAndName))
      {
       System.IO.File.Delete(PathAndName);
      }
      sw = File.CreateText(PathAndName);
      sw.WriteLine(strHtml);
      sw.Close();
      //生成成功
      bSuccess = true;
     }
     catch
     {
       
     
     }
        
     return bSuccess;
   
        
    }



    public void    threadWriteFile()
    {
  
  
     StreamReader sr;
     StreamWriter sw;
     try 
     {
      WebRequest HttpWebRequest = WebRequest.Create(transUrl);
      WebResponse HttpWebResponse = HttpWebRequest.GetResponse();
      sr = new StreamReader(HttpWebResponse.GetResponseStream());
      string strHtml = sr.ReadToEnd();
      sr.Close();     
      if(!System.IO.Directory.Exists(path))
      {
    
       System.IO.Directory.CreateDirectory(path);
      }
      string PathAndName = path + fileName;
      if (System.IO.File.Exists(PathAndName))
      {
       System.IO.File.Delete(PathAndName);
      }
      sw = File.CreateText(PathAndName);
      sw.WriteLine(strHtml);
      sw.Close();
   
     }
     catch
     {
       
     
     }
        
   
  
    }

    /// <summary>
    /// 删除生成文件
    /// </summary>
    /// <param name="path">文件真实路径</param>
    /// <param name="fileName">文件名</param>
    /// <returns>布尔值</returns>
    public static bool delFile(string path,string fileName)
    {
  
     //是否成功的标志
     bool bSuccess=false;
     try
     {
      string pathAndName=path+fileName;
   
      if(System.IO.File.Exists(pathAndName))
      {
    
       System.IO.File.Delete(pathAndName);
    
       bSuccess=true;
    
      }
   
   
     }
     catch
     {
   
   
     }
  
  
    return bSuccess;
  
  
    }



}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多