如何监控文化夹下有txt生成

kfmao2004 2017-11-27 09:11:38
我的D:\Parg会随机由另外的程序生成TXT文件(时间 是不固定的),我要去取这个TXT。用完我就会把这个TXT删了。等侍下一个TXT生成。 我现在设计的是每30分钟去D:\Parg里找TXT。但我感觉这个方法不太好。如果可以做成D:\Parg一有TXT生成。我就去取。就更好了
...全文
345 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
kfmao2004 2017-11-28
  • 打赏
  • 举报
回复
引用 14 楼 starfd 的回复:
你没装vs,还是没在+=后面直接tab帮你生成方法块……

谢谢了,已经解决了
kfmao2004 2017-11-28
  • 打赏
  • 举报
回复
引用 13 楼 xuzuning 的回复:
手写事件订阅表达式时,可利用 VS 提供的功能,而不需记忆长长的类型名
键入 fsw.Created += 后按一次 Tab 键就会产生 new FileSystemEventHandler(方法名),再按一次 Tab 键,就生成了处理方法框架

汗,学习了,还可以这样。谢谢
  • 打赏
  • 举报
回复
你没装vs,还是没在+=后面直接tab帮你生成方法块……
xuzuning 2017-11-28
  • 打赏
  • 举报
回复
手写事件订阅表达式时,可利用 VS 提供的功能,而不需记忆长长的类型名 键入 fsw.Created += 后按一次 Tab 键就会产生 new FileSystemEventHandler(方法名),再按一次 Tab 键,就生成了处理方法框架
xuzuning 2017-11-28
  • 打赏
  • 举报
回复
private void OnProcess(object sender,FileSystemEventArgs e)
kfmao2004 2017-11-28
  • 打赏
  • 举报
回复
引用 10 楼 starfd 的回复:
Created是创建就立刻触发的,所以你如果再事件中立刻就去读取文件的话,可能出现文件被占用,所以你应该延迟一段时间后再去读取,保证不会异常

有点懵,我才自学C#.一个月,如果在button1_Click事件中,要怎么写呢?我试着写了一下。
提示错误 “OnProcess”的重载均与委托“System.IO.FileSystemEventHandler”不匹配
能帮忙看一下不
private void button1_Click(object sender, EventArgs e)
{
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = "D:\\Prag";
fsw.Filter = "*.txt";
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
fsw.Created += new FileSystemEventHandler(OnProcess);
fsw.EnableRaisingEvents = true;
fsw.IncludeSubdirectories = true;
}
private void OnProcess(object sender, FileSystemWatcher e)
{


}
  • 打赏
  • 举报
回复
FileSystemWatcher用法就那么几句话,固定的,只要注意事件通知会重复通知就行了(好像只有Change事件会重复通知,因为以前用的是监控文件变更,创建是否会重复触发事件你可以实际看看)
易2017 2017-11-27
  • 打赏
  • 举报
回复
直接拖控件改属性吧
易2017 2017-11-27
  • 打赏
  • 举报
回复
引用 3 楼 kfmao2004 的回复:
[quote=引用 2 楼 alexshyong 的回复:] 这个帖子说得很详细:http://blog.csdn.net/hwt0101/article/details/8469285 FileSystemWatcher fsw= new FileSystemWatcher(); fsw.Path = “D:\\Parg”; //设置监控的文件目录 fsw.IncludeSubdirectories = true; //设置监控C盘目录下的所有子目录 fsw.Filter = "*.txt"; //设置监控文件的类型 fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size; fsw.Created += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle); //绑定事件触发后处理数据的方法。 fsw.EnableRaisingEvents = true; //启动监控 如果需要同时监控多个文件(如监控系统全盘),只需要创建FileSystemWatcher数组,每个文件用一个FileSystemWatcher进行监控。
能写的具体一点吗?大哥[/quote] 这还不够详细?
易2017 2017-11-27
  • 打赏
  • 举报
回复
引用 1 楼 Runnerchin 的回复:
直接工具箱拖一个FileSystemWatcher就可以了
kfmao2004 2017-11-27
  • 打赏
  • 举报
回复
引用 2 楼 alexshyong 的回复:
这个帖子说得很详细:http://blog.csdn.net/hwt0101/article/details/8469285
FileSystemWatcher fsw= new FileSystemWatcher();
fsw.Path = “D:\\Parg”; //设置监控的文件目录
fsw.IncludeSubdirectories = true; //设置监控C盘目录下的所有子目录
fsw.Filter = "*.txt"; //设置监控文件的类型
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
fsw.Created += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle); //绑定事件触发后处理数据的方法。
fsw.EnableRaisingEvents = true; //启动监控
如果需要同时监控多个文件(如监控系统全盘),只需要创建FileSystemWatcher数组,每个文件用一个FileSystemWatcher进行监控。

能写的具体一点吗?大哥
帅猪儿 2017-11-27
  • 打赏
  • 举报
回复
这个帖子说得很详细:http://blog.csdn.net/hwt0101/article/details/8469285 FileSystemWatcher fsw= new FileSystemWatcher(); fsw.Path = “D:\\Parg”; //设置监控的文件目录 fsw.IncludeSubdirectories = true; //设置监控C盘目录下的所有子目录 fsw.Filter = "*.txt"; //设置监控文件的类型 fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size; fsw.Created += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle); //绑定事件触发后处理数据的方法。 fsw.EnableRaisingEvents = true; //启动监控 如果需要同时监控多个文件(如监控系统全盘),只需要创建FileSystemWatcher数组,每个文件用一个FileSystemWatcher进行监控。
X-i-n 2017-11-27
  • 打赏
  • 举报
回复
直接工具箱拖一个FileSystemWatcher就可以了
  • 打赏
  • 举报
回复
Created是创建就立刻触发的,所以你如果再事件中立刻就去读取文件的话,可能出现文件被占用,所以你应该延迟一段时间后再去读取,保证不会异常
小大飞 2017-11-27
  • 打赏
  • 举报
回复
哇哇哇,又学了一招,好厉害
kfmao2004 2017-11-27
  • 打赏
  • 举报
回复
引用 5 楼 qq_38588710 的回复:
[quote=引用 3 楼 kfmao2004 的回复:]
[quote=引用 2 楼 alexshyong 的回复:]
这个帖子说得很详细:http://blog.csdn.net/hwt0101/article/details/8469285
FileSystemWatcher fsw= new FileSystemWatcher();
fsw.Path = “D:\\Parg”; //设置监控的文件目录
fsw.IncludeSubdirectories = true; //设置监控C盘目录下的所有子目录
fsw.Filter = "*.txt"; //设置监控文件的类型
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
fsw.Created += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle); //绑定事件触发后处理数据的方法。
fsw.EnableRaisingEvents = true; //启动监控
如果需要同时监控多个文件(如监控系统全盘),只需要创建FileSystemWatcher数组,每个文件用一个FileSystemWatcher进行监控。

能写的具体一点吗?大哥[/quote]
这还不够详细?[/quote]

下面的事件,我要怎么写,当TXT被创建时,触发的是Created事件。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧