C#编写控制台程序,实时监测某个文件夹内是否有文件增加 10
2个回答
展开全部
这种代码我还真写过!
就不帮你修改了 你自己改改就能用啦
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class FSWControl : Form
{
static FileSystemWatcher watcher = new FileSystemWatcher();
public FSWControl()
{
InitializeComponent();
string StrPath = ReadrXML("watchdirectory", "savedirectory");
WatcherStrat(StrPath,"*.*",true,true);
}
/// <summary>
/// 初始化监听
/// </summary>
/// <param name="StrWarcherPath">需要监听的目录</param>
/// <param name="FilterType">需要监听的文件类型(筛选器字符串)</param>
/// <param name="IsEnableRaising">是否启用监听</param>
/// <param name="IsInclude">是否监听子目录</param>
private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
{
//初始化监听
watcher.BeginInit();
//设置监听文件类型
watcher.Filter = FilterType;
//设置是否监听子目录
watcher.IncludeSubdirectories = IsInclude;
//设置是否启用监听?
watcher.EnableRaisingEvents = IsEnableRaising;
//设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
//设置监听的路径
watcher.Path = StrWarcherPath;
//注册创建文件或目录时的监听事件
watcher.Created += new FileSystemEventHandler(watch_created);
//注册当指定目录的文件或者目录发生改变的时候的监听事件
watcher.Changed += new FileSystemEventHandler(watch_changed);
//注册当删除目录的文件或者目录的时候的监听事件
watcher.Deleted += new FileSystemEventHandler(watch_deleted);
//当指定目录的文件或者目录发生重命名的时候的监听事件
watcher.Renamed += new RenamedEventHandler(watch_renamed);
//结束初始化
watcher.EndInit();
}
/// <summary>
/// 创建文件或者目录时的监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_created(object sender, FileSystemEventArgs e)
{
//事件内容
}
/// <summary>
/// 当指定目录的文件或者目录发生改变的时候的监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_changed(object sender, FileSystemEventArgs e)
{
//事件内容
}
/// <summary>
/// 当删除目录的文件或者目录的时候的监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_deleted(object sender, FileSystemEventArgs e)
{
//事件内容
}
/// <summary>
/// 当指定目录的文件或者目录发生重命名的时候的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_renamed(object sender, RenamedEventArgs e)
{
//事件内容
}
/// <summary>
/// 启动或者停止监听
/// </summary>
/// <param name="IsEnableRaising">True:启用监听,False:关闭监听</param>
private void WatchStartOrSopt(bool IsEnableRaising)
{
watcher.EnableRaisingEvents = IsEnableRaising;
}
/// <summary>
/// 读取配置文档
/// </summary>
/// <param name="StrNode">节点名字</param>
/// <param name="StrAttribute">属性键</param>
/// <returns>属性值</returns>
private string ReadrXML(string StrNode,string StrAttribute)
{
string returnValue = "";
XmlDocument xmldoc = new XmlDocument();
string StrPath = AppDomain.CurrentDomain.BaseDirectory;
xmldoc.Load(StrPath + "xml/Config.xml");
XmlNodeList nodeList = xmldoc.ChildNodes;
foreach (XmlNode item in nodeList)
{
if (item.Name.Trim().Equals("swfconvert"))
{
nodeList = item.ChildNodes;
foreach (XmlNode nodeitem in nodeList)
{
if (nodeitem.Name == StrNode)
{
returnValue = nodeitem[StrAttribute].Value;
return returnValue;
}
}
}
}
return null;
}
}
}
就不帮你修改了 你自己改改就能用啦
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class FSWControl : Form
{
static FileSystemWatcher watcher = new FileSystemWatcher();
public FSWControl()
{
InitializeComponent();
string StrPath = ReadrXML("watchdirectory", "savedirectory");
WatcherStrat(StrPath,"*.*",true,true);
}
/// <summary>
/// 初始化监听
/// </summary>
/// <param name="StrWarcherPath">需要监听的目录</param>
/// <param name="FilterType">需要监听的文件类型(筛选器字符串)</param>
/// <param name="IsEnableRaising">是否启用监听</param>
/// <param name="IsInclude">是否监听子目录</param>
private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
{
//初始化监听
watcher.BeginInit();
//设置监听文件类型
watcher.Filter = FilterType;
//设置是否监听子目录
watcher.IncludeSubdirectories = IsInclude;
//设置是否启用监听?
watcher.EnableRaisingEvents = IsEnableRaising;
//设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
//设置监听的路径
watcher.Path = StrWarcherPath;
//注册创建文件或目录时的监听事件
watcher.Created += new FileSystemEventHandler(watch_created);
//注册当指定目录的文件或者目录发生改变的时候的监听事件
watcher.Changed += new FileSystemEventHandler(watch_changed);
//注册当删除目录的文件或者目录的时候的监听事件
watcher.Deleted += new FileSystemEventHandler(watch_deleted);
//当指定目录的文件或者目录发生重命名的时候的监听事件
watcher.Renamed += new RenamedEventHandler(watch_renamed);
//结束初始化
watcher.EndInit();
}
/// <summary>
/// 创建文件或者目录时的监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_created(object sender, FileSystemEventArgs e)
{
//事件内容
}
/// <summary>
/// 当指定目录的文件或者目录发生改变的时候的监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_changed(object sender, FileSystemEventArgs e)
{
//事件内容
}
/// <summary>
/// 当删除目录的文件或者目录的时候的监听事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_deleted(object sender, FileSystemEventArgs e)
{
//事件内容
}
/// <summary>
/// 当指定目录的文件或者目录发生重命名的时候的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void watch_renamed(object sender, RenamedEventArgs e)
{
//事件内容
}
/// <summary>
/// 启动或者停止监听
/// </summary>
/// <param name="IsEnableRaising">True:启用监听,False:关闭监听</param>
private void WatchStartOrSopt(bool IsEnableRaising)
{
watcher.EnableRaisingEvents = IsEnableRaising;
}
/// <summary>
/// 读取配置文档
/// </summary>
/// <param name="StrNode">节点名字</param>
/// <param name="StrAttribute">属性键</param>
/// <returns>属性值</returns>
private string ReadrXML(string StrNode,string StrAttribute)
{
string returnValue = "";
XmlDocument xmldoc = new XmlDocument();
string StrPath = AppDomain.CurrentDomain.BaseDirectory;
xmldoc.Load(StrPath + "xml/Config.xml");
XmlNodeList nodeList = xmldoc.ChildNodes;
foreach (XmlNode item in nodeList)
{
if (item.Name.Trim().Equals("swfconvert"))
{
nodeList = item.ChildNodes;
foreach (XmlNode nodeitem in nodeList)
{
if (nodeitem.Name == StrNode)
{
returnValue = nodeitem[StrAttribute].Value;
return returnValue;
}
}
}
}
return null;
}
}
}
追问
这个网上我看到过 谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询