当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  事件驱动自动生成静态页面模板解决方法(一)——利用静态类和FileSystemWatcher

 事件驱动自动生成静态页面模板解决方法(一)——利用静态类和FileSystemWatcher

点击次数:20 次 发布日期:2008-11-26 09:53:01 作者:源代码网
源代码网推荐     
源代码网推荐  网站页面模板最大的用处大概就是生成静态页面,当然也可以动态模板切换实现页面风格变化,其他的应用看想法的多少了。
源代码网推荐  
源代码网推荐  这里我利用静态类和FileSystemWatcher实现网页模板的提取功能。
源代码网推荐  
源代码网推荐  利用静态类和FileSystemWatcher实现有以下优点:
源代码网推荐  1、模板存放在内存中读取方便,提高读取速度。
源代码网推荐  2、动态监视模板改动,模板实时更新。
源代码网推荐  3、通过一个XML文件作为模板管理,简单,方便。
源代码网推荐  4、所有模板保存为单独文件方便修改。
源代码网推荐  
源代码网推荐  最优应用:
源代码网推荐  由事件驱动自动生成静态页面或者直接根据数据内容生成静态页面。
源代码网推荐  例如:
源代码网推荐  1、定时抓取一些网站的内容。(PS:看到好多网站在抓Cnblogs的信息,我并不希望这些网站利用这个方法。)
源代码网推荐  2、实时新闻信息生成。
源代码网推荐  3、信息源驱动信息生成。
源代码网推荐  
源代码网推荐  提取方法:
源代码网推荐  GetTemplateByKey(模板key);
源代码网推荐  
源代码网推荐  注意事项:
源代码网推荐  1、模板列表XML文件最好更改后缀名,这样防止其他人直接访问XML文件得到您的列表。
源代码网推荐  2、模板文件也最好更改后缀名,这样防止其他人直接访问HTML文件得到您的模板。
源代码网推荐  
源代码网推荐  模板调用类代码
源代码网推荐  cs --------------------------------------------------------------------------------
源代码网推荐  
源代码网推荐  using System;
源代码网推荐  using System.Collections.Generic;
源代码网推荐  using System.Text;
源代码网推荐  using System.IO;
源代码网推荐  using System.Data;
源代码网推荐  using System.Web;
源代码网推荐  
源代码网推荐  namespace Xingmai..Template
源代码网推荐  {
源代码网推荐   /// <summary>
源代码网推荐   /// Html模板调用
源代码网推荐   /// </summary>
源代码网推荐   public static class HtmlCollection
源代码网推荐   {
源代码网推荐   /// <summary>
源代码网推荐   /// 模板表
源代码网推荐   /// </summary>
源代码网推荐   private static DataTable Collection = new DataTable();
源代码网推荐  
源代码网推荐   /// <summary>
源代码网推荐   /// 模板目录
源代码网推荐   /// </summary>
源代码网推荐   private static string strTemplatePath;
源代码网推荐   #region 静态构造函数
源代码网推荐  
源代码网推荐   static HtmlCollection()
源代码网推荐   {
源代码网推荐   LoadTemplates();
源代码网推荐   }
源代码网推荐  
源代码网推荐   #endregion
源代码网推荐  
源代码网推荐   /// <summary>
源代码网推荐   /// 初始化数据表并设置文件监视
源代码网推荐   /// </summary>
源代码网推荐   private static void LoadTemplates()
源代码网推荐   {
源代码网推荐   strTemplatePath = HttpContext.Current.Server.MapPath("~/TemplateDir/HtmlTemplate/");
源代码网推荐   FileSystemWatcher watcher = new FileSystemWatcher(strTemplatePath, "*.*");
源代码网推荐   watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
源代码网推荐   watcher.IncludeSubdirectories = true;
源代码网推荐   watcher.Changed += new FileSystemEventHandler(watcher_Changed);
源代码网推荐   LoadTemplateData();
源代码网推荐   watcher.EnableRaisingEvents = true;
源代码网推荐   }
源代码网推荐  
源代码网推荐   /// <summary>
源代码网推荐   /// 更新数据
源代码网推荐   /// </summary>
源代码网推荐   private static void LoadTemplateData()
源代码网推荐   {
源代码网推荐   lock (Collection)
源代码网推荐   {
源代码网推荐   DataSet ds = new DataSet();
源代码网推荐   ds.ReadXml(string.Format("{0}Template.List", strTemplatePath));
源代码网推荐   if (Collection != null)
源代码网推荐   {
源代码网推荐   Collection = null;
源代码网推荐   }
源代码网推荐   Collection = ds.Tables[0];
源代码网推荐   Collection.Columns.Add("Content");
源代码网推荐   string strTemplateFilePath = strTemplatePath + @"Template";
源代码网推荐   foreach (DataRow dr in Collection.Rows)
源代码网推荐   {
源代码网推荐   using (TextReader reader = new StreamReader(string.Format("{0}{1}", strTemplateFilePath, dr["File"].ToString())))
源代码网推荐   {
源代码网推荐   dr["Content"] = reader.ReadToEnd();
源代码网推荐   }
源代码网推荐  
源代码网推荐   }
源代码网推荐   }
源代码网推荐   }
源代码网推荐  
源代码网推荐   /// <summary>
源代码网推荐   /// 如果文件夹有改变
源代码网推荐   /// </summary>
源代码网推荐   /// <param name="sender"></param>
源代码网推荐   /// <param name="e"></param>
源代码网推荐   static void watcher_Changed(object sender, FileSystemEventArgs e)
源代码网推荐   {
源代码网推荐   LoadTemplateData();
源代码网推荐   }
源代码网推荐  
源代码网推荐   /// <summary>
源代码网推荐   /// 通过Key获取相应的模板
源代码网推荐   /// </summary>
源代码网推荐   /// <param name="strKey"></param>
源代码网推荐   /// <returns></returns>
源代码网推荐   public static string GetTemplateByKey(string strKey)
源代码网推荐   {
源代码网推荐   lock (Collection)
源代码网推荐   {
源代码网推荐   DataView dv = Collection.DefaultView;
源代码网推荐   dv.RowFilter = string.Format("Key="{0}"", strKey);
源代码网推荐   if (dv.Count == 0)
源代码网推荐   return "";
源代码网推荐   else
源代码网推荐   return dv[0]["Content"].ToString();
源代码网推荐   }
源代码网推荐   }
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐  
源代码网推荐  xml 文件(根据自己的需要添加字段,如果模板较多可以添加分类字段)-----------------------------------------------------
源代码网推荐  
源代码网推荐  ---
源代码网推荐  <List>
源代码网推荐  <Item>
源代码网推荐  <Key>1</key>
源代码网推荐  <Name>1</Name>
源代码网推荐  <File>1.html</File>
源代码网推荐  </List>
源代码网推荐  
源代码网推荐  关于FileSystemWatcher (MSDN)
源代码网推荐  
源代码网推荐  侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。
源代码网推荐  
源代码网推荐  使用 FileSystemWatcher 监视指定目录中的更改。可监视指定目录中的文件或子目录的更改。可以创建一个组件来监视本地计算机、
源代码网推荐  
源代码网推荐  网络驱动器或远程计算机上的文件。
源代码网推荐  
源代码网推荐  若要监视所有文件中的更改,请将 Filter 属性设置为空字符串 ("") 或使用通配符(“*.*”)。若要监视特定的文件,请将 Filter
源代码网推荐  
源代码网推荐  属性设置为该文件名。例如,若要监视文件 MyDoc.txt 中的更改,请将 Filter 属性设置为“MyDoc.txt”。也可以监视特定类型文件
源代码网推荐  
源代码网推荐  中的更改。例如,若要监视文本文件中的更改,请将 Filter 属性设置为“*.txt”。
源代码网推荐  
源代码网推荐  可监视目录或文件中的若干种更改。例如,可监视文件或目录的 Attributes、LastWrite 日期和时间或 Size 方面的更改。通过将
源代码网推荐  
源代码网推荐  NotifyFilter 属性设置为 NotifyFilters 值之一来达到此目的。有关可监视的更改类型的更多信息,请参见 NotifyFilters。
源代码网推荐  
源代码网推荐  可监视文件或目录的重命名、删除或创建。例如,若要监视文本文件的重命名,请将 Filter 属性设置为“*.txt”,并使用为其参数
源代码网推荐  
源代码网推荐  指定的 Renamed 来调用 WaitForChanged 方法。
源代码网推荐  
源代码网推荐  Windows 操作系统在 FileSystemWatcher 创建的缓冲区中通知组件发生文件更改。如果短时间内有很多更改,则缓冲区可能会溢出。
源代码网推荐  
源代码网推荐  这将导致组件失去对目录更改的跟踪,并且它将只提供一般性通知。使用 InternalBufferSize 属性来增加缓冲区大小的开销较大,因
源代码网推荐  
源代码网推荐  为它来自无法换出到磁盘的非页面内存,所以应确保缓冲区大小适中(尽量小,但也要有足够大小以便不会丢失任何文件更改事件)。
源代码网推荐  
源代码网推荐  若要避免缓冲区溢出,请使用 NotifyFilter 和 IncludeSubdirectories 属性,以便可筛选掉不想要的更改通知。
源代码网推荐  
源代码网推荐  有关 FileSystemWatcher 的实例的初始属性值列表,请参见 FileSystemWatcher 构造函数。
源代码网推荐  
源代码网推荐  使用 FileSystemWatcher 类时,请注意以下事项。
源代码网推荐  
源代码网推荐  不忽略隐藏文件。
源代码网推荐  
源代码网推荐  在某些系统中,FileSystemWatcher 使用 8.3 短文件名格式报告文件更改。例如,对“LongFileName.LongExtension”的更改可能报
源代码网推荐  
源代码网推荐  告为“LongFi~.Lon”。
源代码网推荐  
源代码网推荐  此类在类级别上包含一个链接要求和一个继承要求,这两个要求应用于所有成员。如果直接调用方或派生类不具有完全信任权限,则会
源代码网推荐  
源代码网推荐  引发 SecurityException。有关安全要求的详细信息,请参见 链接要求。
源代码网推荐  
源代码网推荐  事件和缓冲区大小
源代码网推荐  请注意,有几个因素可能影响引发哪些文件系统更改事件,如下所述:
源代码网推荐  
源代码网推荐  公共文件系统操作可能会引发多个事件。例如,将文件从一个目录移到另一个目录时,可能会引发若干 OnChanged 以及一些
源代码网推荐  
源代码网推荐  OnCreated 和 OnDeleted 事件。移动文件是一个包含多个简单操作的复杂操作,因此会引发多个事件。同样,一些应用程序(如反病
源代码网推荐  
源代码网推荐  毒软件)可能导致被 FileSystemWatcher 检测到的附加文件系统事件。
源代码网推荐  
源代码网推荐  只要磁盘没有切换或移除,FileSystemWatcher 就可监视它们。因为 CD 和 DVD 的时间戳和属性不能更改,所以 FileSystemWatcher
源代码网推荐  
源代码网推荐  不为 CD 和 DVD 引发事件。要使该组件正常运行,远程计算机必须具有所需的这些平台之一。但是,无法从 Windows NT 4.0 计算机
源代码网推荐  
源代码网推荐  监视远程 Windows NT 4.0 计算机。
源代码网推荐  
源代码网推荐  在 Windows XP(Service Pack 1 之前版本)或者 Windows 2000 SP2 或更低版本中,如果多个 FileSystemWatcher 对象正在监视同
源代码网推荐  
源代码网推荐  一个 UNC 路径,则只有其中一个对象会引发事件。在运行 Windows XP SP1 及之后版本、Windows 2000 SP3 或之后版本或者 Windows
源代码网推荐  
源代码网推荐  Server 2003 的计算机上,所有 FileSystemWatcher 对象都将引发相应的事件。
源代码网推荐  
源代码网推荐  设置 Filter 不会减少进入缓冲区中的内容。
源代码网推荐  
源代码网推荐  请注意,由于 Windows 操作系统的依赖项,当丢失某个事件或超出缓冲区大小时,FileSystemWatcher 不会引发 Error 事件。若要防
源代码网推荐  
源代码网推荐  止丢失事件,请遵从这些准则:
源代码网推荐  
源代码网推荐  使用 InternalBufferSize 属性增加缓冲区大小可以防止丢失文件系统更改事件。
源代码网推荐  
源代码网推荐  避免监视带有长文件名的文件。考虑使用较短的名称进行重命名。
源代码网推荐  
源代码网推荐  尽可能使事件处理代码短小。
源代码网推荐  
源代码网推荐  示例
源代码网推荐  下面的示例创建 FileSystemWatcher 来监视运行时指定的目录。组件设置为监视 LastWrite 和 LastAccess 时间方面的更改,以及目
源代码网推荐  
源代码网推荐  录中文本文件的创建、删除或重命名。如果更改、创建或删除文件,文件路径将被输出到控制台。在文件重命名后,旧路径和新路径都
源代码网推荐  
源代码网推荐  输出到控制台。
源代码网推荐  
源代码网推荐  在此示例中使用 System.Diagnostics 和 System.IO 命名空间。
源代码网推荐  
源代码网推荐  public class Watcher
源代码网推荐  {
源代码网推荐  
源代码网推荐   public static void Main()
源代码网推荐   {
源代码网推荐   Run();
源代码网推荐  
源代码网推荐   }
源代码网推荐  
源代码网推荐   [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
源代码网推荐   public static void Run()
源代码网推荐   {
源代码网推荐   string[] args = System.Environment.GetCommandLineArgs();
源代码网推荐  
源代码网推荐   // If a directory is not specified, exit program.
源代码网推荐   if(args.Length != 2)
源代码网推荐   {
源代码网推荐   // Display the proper way to call the program.
源代码网推荐   Console.WriteLine("Usage: Watcher.exe (directory)");
源代码网推荐   return;
源代码网推荐   }
源代码网推荐  
源代码网推荐   // Create a new FileSystemWatcher and set its properties.
源代码网推荐   FileSystemWatcher watcher = new FileSystemWatcher();
源代码网推荐   watcher.Path = args[1];
源代码网推荐   /* Watch for changes in LastAccess and LastWrite times, and
源代码网推荐   the renaming of files or directories. */
源代码网推荐   watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
源代码网推荐   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
源代码网推荐   // Only watch text files.
源代码网推荐   watcher.Filter = "*.txt";
源代码网推荐  
源代码网推荐   // Add event handlers.
源代码网推荐   watcher.Changed += new FileSystemEventHandler(OnChanged);
源代码网推荐   watcher.Created += new FileSystemEventHandler(OnChanged);
源代码网推荐   watcher.Deleted += new FileSystemEventHandler(OnChanged);
源代码网推荐   watcher.Renamed += new RenamedEventHandler(OnRenamed);
源代码网推荐  
源代码网推荐   // Begin watching.
源代码网推荐   watcher.EnableRaisingEvents = true;
源代码网推荐  
源代码网推荐   // Wait for the user to quit the program.
源代码网推荐   Console.WriteLine("Press "q" to quit the sample.");
源代码网推荐   while(Console.Read()!="q");
源代码网推荐   }
源代码网推荐  
源代码网推荐   // Define the event handlers.
源代码网推荐   private static void OnChanged(object source, FileSystemEventArgs e)
源代码网推荐   {
源代码网推荐   // Specify what is done when a file is changed, created, or deleted.
源代码网推荐   Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
源代码网推荐   }
源代码网推荐  
源代码网推荐   private static void OnRenamed(object source, RenamedEventArgs e)
源代码网推荐   {
源代码网推荐   // Specify what is done when a file is renamed.
源代码网推荐   Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华