简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数据库。

原需求:原先的需求是这样的:有一台PDA扫码枪,一个IP照相机放置在下线区传送带上方。当PDA扫描箱子上的条码,触发相机拍照,将图片流传至远端服务器,找到对应的条码,将图片存储并更新数据库。

然而我不知道PDA扫描的瞬间如何与IP相机通信(蓝牙或WLAN?),其实关键是我不知道怎样使用IP相机的外触发功能,增加蓝牙触发器?也不知道怎样hack或ssh到这个相机(应该是linux的吧),所以只能先使用简化需求的版本。

而简化需求的版本,关键就是监视文件夹内容变化与上传文件流。

昨天问了下度娘,C#中的监视组件名字叫做FileSystemWatcher。

于是写了个demo,可以监视所有逻辑盘或者某个文件夹。

使用方法:

1.直接打开是监视所有逻辑磁盘文件变化。

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

源代码:

 namespace FileSystemWatcherDemo
{
class Program
{
static void Main(string[] args)
{
//watcher组
FileSystemWatcher[] watchers; //若未传递参数,则监视所有文件系统,包括CD-ROM(不可用),可移动磁盘(不可用)等
if (args.Length == )
{
string[] drivers = Directory.GetLogicalDrives();
watchers = new FileSystemWatcher[drivers.Length]; for (int i = ; i < drivers.Length; i++)
{
try
{
watchers[i] = new FileSystemWatcher { Path = drivers[i] };
}
catch (Exception ex)
{
Trace.TraceWarning(ex.Message);
}
}
}
else
{
watchers = new FileSystemWatcher[];
watchers[] = new FileSystemWatcher { Path = args[] };
} foreach (FileSystemWatcher w in watchers)
{
if (w == null) continue; w.Filter = "*";
w.IncludeSubdirectories = true;
w.EnableRaisingEvents = true; w.Created += onFileSystem_Changed;
w.Deleted += onFileSystem_Changed;
w.Changed += onFileSystem_Changed;
w.Renamed += watcher_Renamed;
} Console.ReadLine();
} #region [ 检测文件是否占用 ]
/// <summary>
/// 检测文件是否占用
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
static bool IsFileReady(string filename)
{
var fi = new FileInfo(filename);
FileStream fs = null;
try
{
fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None);
return true;
}
catch (IOException)
{
return false;
} finally
{
if (fs != null)
fs.Close();
}
}
#endregion private static volatile object _lock = true;
static void onFileSystem_Changed(object sender, FileSystemEventArgs e)
{
lock (_lock)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("[");
Console.Write(DateTime.Now.ToString("HH:mm:ss"));
Console.Write("] "); switch (e.ChangeType.ToString().ToLower())
{
case "created":
//while (!IsFileReady(e.FullPath))
//{
// if (!File.Exists(e.FullPath))
// return;
// Thread.Sleep(100);
//}
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(e.ChangeType);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ");
Console.Write(e.Name);
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(e.FullPath); break;
case "deleted":
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(e.ChangeType);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ");
Console.Write(e.Name);
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(e.FullPath);
break;
case "changed":
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(e.ChangeType);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ");
Console.Write(e.Name);
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(e.FullPath);
break;
} Console.Write("\r\n");
}
}
static void watcher_Renamed(object sender, RenamedEventArgs e)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(e.ChangeType);
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ");
Console.Write(e.OldName);
Console.Write(e.OldFullPath);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(" ");
Console.Write(e.Name);
Console.Write(e.FullPath);
Console.Write(Thread.CurrentThread.Name);
Console.Write("\r\n");
}
}
}

仍有bug,望高手指正。

附上编译好的exe,可以直接运行。

C# FileSystemWatcher 监视磁盘文件变更的更多相关文章

  1. C# FileSystemWatcher 监视磁盘文件

    C# FileSystemWatcher 监视磁盘文件变更 简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数 ...

  2. FileSystemWatcher 监视指定目录中的变更

    .Net框架类库中的FileSystemWatcher如它的名称一样是一个用于监视文件系统变化的一个控件.使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录 ...

  3. 使用FileSystemWatcher监视指定目录

    使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录的更改. 以下是一个简单的实例,用来监控指定目录下文件的新增.删除.重命名等情况(文件内容更改会触发多次, ...

  4. 使用FileSystemWatcher监视文件变化

    本文转载:http://www.cnblogs.com/zanxiaofeng/archive/2011/01/08/1930583.html FileSystemWatcher基础 属性: Path ...

  5. 利用FileSystemWatcher实现磁盘文件监控

    马上放假了,好开森啊O(∩_∩)O哈哈~ ——————————————————————————————————————————————————————— 昨天逛园子,发现了一个FileSystemWa ...

  6. C#使用FileSystemWatcher控件实现的文件监控功能示例

    本文实例讲述了C#使用FileSystemWatcher控件实现的文件监控功能.分享给大家供大家参考,具体如下: FileSystemWatcher 可以使用FileSystemWatcher组件监视 ...

  7. 文件读写(二)利用SteamReader和StreamWrite类处理字符串、FileSystemWatcher、BinaryReader/BinaryWriter

    一.读写类: TextReader/TextWriter:文本读写,抽象类 TextReader,其派生类: StreamReader:以一种特定的编码从字节流中读取字符. StringReader: ...

  8. C#实现对文件目录的实时监控

    本文主要描述如何通过C#实现实时监控文件目录下的变化,包括文件和目录的添加,删除,修改和重命名等操作. 首先,我们需要对.net提供的FileSystemWatcher类有所了解.我有些懒,找了MSD ...

  9. React Native环境搭建以及几个基础控件的使用

    之前写了几篇博客,但是没有从最基础的开始写,现在想了想感觉不太合适,所以现在把基础的一些东西给补上,也算是我从零开始学习RN的经验吧! 一.环境搭建 首先声明一下,本人现在用的编辑器是SublimeT ...

随机推荐

  1. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  2. 关于在WIN32调用一些Zw系列的文件操作函数

    转自:http://blog.csdn.net/cooblily/archive/2007/10/27/1848037.aspx 都好久沒上來写文章了,都不知道做什么好,結果还是学写了一下用Nativ ...

  3. 17.2.2 Replication Relay and Status Logs 复制Relay 和状态日志;

    17.2.2 Replication Relay and Status Logs 复制Relay 和状态日志: 17.2.2.1 The Slave Relay Log 17.2.2.2 Slave ...

  4. git ignore 的使用

    http://www.barretlee.com/blog/2015/09/06/set-gitignore-after-add-file/ 需要注意的 **: 如果一个 pattern 以 ** 开 ...

  5. Learning WCF Chapter2 WCF Contracts and Serialization

    So far I’ve talked about the standards behind it all,but in fact WCF hides most of this from the dev ...

  6. Android4.0 -- UI控件之 Menu 菜单的的使用(三)

    上一讲 [Android 开发]:UI控件之 Menu 菜单的的使用(二) 我们讲解了创建上下文菜单的第一种使用方式:Creating a floating context menu [创建悬浮的上下 ...

  7. opengl学习

    #include"stdafx.h" #define GLUT_DISABLE_ATEXIT_HACK #include <GL/glut.h> //glut自动将gl ...

  8. Bash字符串的处理

    # 定义几个变量用于测试目的 FILE_NAME="/usr/local/app/world.txt" TIME_NOW="`date +"%Y%m%d%H%M ...

  9. ARM学习笔记12——GNU ARM汇编伪操作

    1..section 1.1.语法格式 .section section_name[,"flags"[,%type[,flag_specific_arguments]]] 1.2. ...

  10. CSS浏览器兼容性----Hack

    CSS Hack大致有3种表现形式,CSS类内部Hack.选择器Hack以及HTML头部引用(if IE)Hack,CSS Hack主要针对IE浏览器.类内部Hack:比如 IE6能识别下划线&quo ...