C# FileSystemWatcher 监视磁盘文件
C# FileSystemWatcher 监视磁盘文件变更
简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数据库。
原需求:原先的需求是这样的:有一台PDA扫码枪,一个IP照相机放置在下线区传送带上方。当PDA扫描箱子上的条码,触发相机拍照,将图片流传至远端服务器,找到对应的条码,将图片存储并更新数据库。
然而我不知道PDA扫描的瞬间如何与IP相机通信(蓝牙或WLAN?),其实关键是我不知道怎样使用IP相机的外触发功能,增加蓝牙触发器?也不知道怎样hack或ssh到这个相机(应该是linux的吧),所以只能先使用简化需求的版本。
而简化需求的版本,关键就是监视文件夹内容变化与上传文件流。
昨天问了下度娘,C#中的监视组件名字叫做FileSystemWatcher。
于是写了个demo,可以监视所有逻辑盘或者某个文件夹。
使用方法:
1.直接打开是监视所有逻辑磁盘文件变化。

2.或者传递参数,监视某一路径文件变化。如图,监视e盘

源代码:

1 namespace FileSystemWatcherDemo
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 //watcher组
8 FileSystemWatcher[] watchers;
9
10 //若未传递参数,则监视所有文件系统,包括CD-ROM(不可用),可移动磁盘(不可用)等
11 if (args.Length == 0)
12 {
13 string[] drivers = Directory.GetLogicalDrives();
14 watchers = new FileSystemWatcher[drivers.Length];
15
16 for (int i = 0; i < drivers.Length; i++)
17 {
18 try
19 {
20 watchers[i] = new FileSystemWatcher { Path = drivers[i] };
21 }
22 catch (Exception ex)
23 {
24 Trace.TraceWarning(ex.Message);
25 }
26 }
27 }
28 else
29 {
30 watchers = new FileSystemWatcher[1];
31 watchers[0] = new FileSystemWatcher { Path = args[0] };
32 }
33
34 foreach (FileSystemWatcher w in watchers)
35 {
36 if (w == null) continue;
37
38 w.Filter = "*";
39 w.IncludeSubdirectories = true;
40 w.EnableRaisingEvents = true;
41
42 w.Created += onFileSystem_Changed;
43 w.Deleted += onFileSystem_Changed;
44 w.Changed += onFileSystem_Changed;
45 w.Renamed += watcher_Renamed;
46 }
47
48 Console.ReadLine();
49 }
50
51 #region [ 检测文件是否占用 ]
52 /// <summary>
53 /// 检测文件是否占用
54 /// </summary>
55 /// <param name="filename"></param>
56 /// <returns></returns>
57 static bool IsFileReady(string filename)
58 {
59 var fi = new FileInfo(filename);
60 FileStream fs = null;
61 try
62 {
63 fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None);
64 return true;
65 }
66 catch (IOException)
67 {
68 return false;
69 }
70
71 finally
72 {
73 if (fs != null)
74 fs.Close();
75 }
76 }
77 #endregion
78
79 private static volatile object _lock = true;
80 static void onFileSystem_Changed(object sender, FileSystemEventArgs e)
81 {
82 lock (_lock)
83 {
84 Console.ForegroundColor = ConsoleColor.DarkGray;
85 Console.Write("[");
86 Console.Write(DateTime.Now.ToString("HH:mm:ss"));
87 Console.Write("] ");
88
89 switch (e.ChangeType.ToString().ToLower())
90 {
91 case "created":
92 //while (!IsFileReady(e.FullPath))
93 //{
94 // if (!File.Exists(e.FullPath))
95 // return;
96 // Thread.Sleep(100);
97 //}
98 Console.ForegroundColor = ConsoleColor.Green;
99 Console.Write(e.ChangeType);
100 Console.ForegroundColor = ConsoleColor.White;
101 Console.Write(" ");
102 Console.Write(e.Name);
103 Console.Write(" ");
104 Console.ForegroundColor = ConsoleColor.DarkGray;
105 Console.Write(e.FullPath);
106
107 break;
108 case "deleted":
109 Console.ForegroundColor = ConsoleColor.Red;
110 Console.Write(e.ChangeType);
111 Console.ForegroundColor = ConsoleColor.White;
112 Console.Write(" ");
113 Console.Write(e.Name);
114 Console.Write(" ");
115 Console.ForegroundColor = ConsoleColor.DarkGray;
116 Console.Write(e.FullPath);
117 break;
118 case "changed":
119 Console.ForegroundColor = ConsoleColor.Cyan;
120 Console.Write(e.ChangeType);
121 Console.ForegroundColor = ConsoleColor.White;
122 Console.Write(" ");
123 Console.Write(e.Name);
124 Console.Write(" ");
125 Console.ForegroundColor = ConsoleColor.DarkGray;
126 Console.Write(e.FullPath);
127 break;
128 }
129
130 Console.Write("\r\n");
131 }
132 }
133 static void watcher_Renamed(object sender, RenamedEventArgs e)
134 {
135 Console.ForegroundColor = ConsoleColor.Magenta;
136 Console.Write(e.ChangeType);
137 Console.ForegroundColor = ConsoleColor.White;
138 Console.Write(" ");
139 Console.Write(e.OldName);
140 Console.Write(e.OldFullPath);
141 Console.ForegroundColor = ConsoleColor.Yellow;
142 Console.Write(" ");
143 Console.Write(e.Name);
144 Console.Write(e.FullPath);
145 Console.Write(Thread.CurrentThread.Name);
146 Console.Write("\r\n");
147 }
148 }
149 }

仍有bug,望高手指正。
附上编译好的exe,可以直接运行。
C# FileSystemWatcher 监视磁盘文件的更多相关文章
- C# FileSystemWatcher 监视磁盘文件变更
简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数据库. 原需求:原先的需求是这样的:有一台PDA扫码枪,一个 ...
- 使用FileSystemWatcher监视指定目录
使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录的更改. 以下是一个简单的实例,用来监控指定目录下文件的新增.删除.重命名等情况(文件内容更改会触发多次, ...
- FileSystemWatcher 监视指定目录中的变更
.Net框架类库中的FileSystemWatcher如它的名称一样是一个用于监视文件系统变化的一个控件.使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录 ...
- 使用FileSystemWatcher监视文件变化
本文转载:http://www.cnblogs.com/zanxiaofeng/archive/2011/01/08/1930583.html FileSystemWatcher基础 属性: Path ...
- 利用FileSystemWatcher实现磁盘文件监控
马上放假了,好开森啊O(∩_∩)O哈哈~ ——————————————————————————————————————————————————————— 昨天逛园子,发现了一个FileSystemWa ...
- C#使用FileSystemWatcher控件实现的文件监控功能示例
本文实例讲述了C#使用FileSystemWatcher控件实现的文件监控功能.分享给大家供大家参考,具体如下: FileSystemWatcher 可以使用FileSystemWatcher组件监视 ...
- 文件读写(二)利用SteamReader和StreamWrite类处理字符串、FileSystemWatcher、BinaryReader/BinaryWriter
一.读写类: TextReader/TextWriter:文本读写,抽象类 TextReader,其派生类: StreamReader:以一种特定的编码从字节流中读取字符. StringReader: ...
- C#实现对文件目录的实时监控
本文主要描述如何通过C#实现实时监控文件目录下的变化,包括文件和目录的添加,删除,修改和重命名等操作. 首先,我们需要对.net提供的FileSystemWatcher类有所了解.我有些懒,找了MSD ...
- grunt打包过程中的注意点
1.安装nodeJS nodeJS下载地址: http://www.nodejs.org/download/ 2. 在Node.js command prompt 这个控制面板输入 npm i ...
随机推荐
- c# Ftp下载程序源代码解析
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- SSH骨架Struts(1)——Struts执行过程
收养Struts骨架Web应用,以举例的方式介绍的基本流程. 一.实例 Login.jsp,进行系统登录的页面 <form action="login.do" method= ...
- 敏感字符串加密处理(PHP实现)
/** * 敏感字符串加密处理 * @param $raw_str 原始字符串 * @param $before 前面保留的显示位数 * @param $after 后面保留的显示位数 * @para ...
- quick-cocos2d-x游戏开发【6】——制作您自己的自定义效果button菜单
前面提到的主菜单使用,还是很easy的,但我们在商业产品.经常看到button他们人很好,照片不仅就好了,和动画也很不错.Candy Crash都玩过吧,他们看到,button.真的像果冻,效果确实非 ...
- easyUI的combobox实现级联
先简介下combobox: easyUI重写了select,取而代之的是combobox,有例如以下几种方式能够创建一个combobox 1.使用select标签,并加上class="eas ...
- XP下类似%windir% %userprofile% 的变量的说明(转)
在一些批处理或者系统技巧操作教程文章中,我们常常会看到一些形如 %windir% 或者 %systemdrive% 的变量.这些变量都代表着什么含义呢?下面小技巧之家为大家整理了在Windows XP ...
- poj1251--Kruskal
/* * poj1251-- Kruskal * date 2014/7/15 * state AC */ #include <iostream> #include <algorit ...
- (转).net webconfig使用IConfigurationSectionHandler自定section
自定义配置结构 (使用IConfigurationSectionHandler) 假设有以下的配置信息,其在MyInfo可以重复许多次,那么应如何读取配置呢?这时就要使用自定义的配置程序了.<m ...
- JAVA类型修饰符(public,protected,private,friendly)
JAVA类型修饰符(public,protected,private,friendly) public的类.类属变量及方法.包内及包外的不论什么类均能够訪问:protected的类.类属变量及方法,包 ...
- UVA11983 - Weird Advertisement(扫描线)
UVA11983 - Weird Advertisement(扫描线) 题目链接 题目大意:给你n个覆盖矩形,问哪些整数点是被覆盖了k次. 题目大意:这题和hdu1542是一个题型.可是这题求的是覆盖 ...