简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并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. 码云分布式之 Brzo 服务器

    摘要: 码云是国内最大的代码托管平台,为了支持更大的用户规模,开发团队也在对一些组件进行大规模的重构. 前言 码云是国内最大的代码托管平台.码云基于 Gitlab 5.5 开发,经过几年的开发已经和官 ...

  2. 17.1.1.5 Creating a Data Snapshot Using mysqldump

    一种方式创建一个数据库的快照在一个存在的master 数据库是使用mysqldump 来创建你需要复制的所有数据库的dump. 一旦数据dump 是完成,你然后倒入数据到slave 在开始复制过程前 ...

  3. linux 下 epoll 编程

    转载自 Linux epoll模型 ,这篇文章讲的非常详细! 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显 ...

  4. Apache Log4j使用实例

    Apache Log4j使用实例  原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.  Blog:  1.Logger类 通过Logger类的静 ...

  5. 【转】Android中intent传递对象和Bundle的用法

    原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202 android中的组件间传递的对象一般实现Parcelable接口,当然也可 ...

  6. 只要把鼠标移上Div方框,方框就自动顺时针旋转

    这是一个CSS3特效,IE下看不到效果.一个Div方框,在CSS3代码的作用下,只要把鼠标移上Div方框,方框就自动顺时针旋转.代码量不大,甚至有些简单,作为一个基础的CSS3实例,我想还是比较不错的 ...

  7. Windows 8 Hyper-V虚拟机功能(转载)

    刚才看见一兄弟w500折腾win8 hyper-v功能,普及下吧,欢迎各位斧正 Windows 8 中 Hyper-V 3.0 的 CPU 支持说明 Windows 8 将直接内置 Hyper-V 3 ...

  8. basic mongodb

    basic mongodb */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Consola," ...

  9. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  10. phonegap与google analytics整合

    用phonegap开发的app接近尾声,需要整一个谷歌分析进去. 1.首先申请一个GA帐号,在“what would you like to track”下选择APP