C # FileSystemWatcher monitors Disk File changes,

Source: Internet
Author: User

C # FileSystemWatcher monitors Disk File changes,

Simplified requirements: A simplified requirement is as follows: a photo program is running. Once captured, the image files are stored in a directory, then the image will be uploaded to the remote server and updated to the database.

Original requirement: the original requirement was as follows: There was a PDA scanner, and an IP camera was placed above the conveyor belt in the deprecation area. When the PDA scans the bar code on the box, the camera is triggered to take a photo, the picture is circulated to the remote server, the corresponding bar code is found, the picture is stored and the database is updated.

 

However, I don't know how to communicate with the IP camera (Bluetooth or WLAN?) when the PDA scans ?), In fact, the key is that I don't know how to use the external trigger function of the IP camera to add a Bluetooth trigger? I don't know how to hack or ssh to this camera (it should be linux), so I only need to use the version to simplify the requirement.

 

To simplify the version, the key is to monitor the changes in folder content and upload file streams.

Yesterday, I asked my mother, and the monitoring component in C # is called FileSystemWatcher.

So I wrote a demo to monitor all logical disks or a folder.

Usage:

1. Directly opening is to monitor all Logical Disk File changes.

2. You can also pass parameters to monitor file changes in a specific path ., Monitor an edisk

 

Source code:

1 namespace FileSystemWatcherDemo 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 // watcher group 8 FileSystemWatcher [] watchers; 9 10 // if no parameters are passed, monitor all file systems, including CD-ROM (unavailable), removable disks (unavailable), and other 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. enabl ERaisingEvents = true; 41 42 w. created + = onFileSystem_Changed; 43 w. deleted + = onFileSystem_Changed; 44 w. changed + = onFileSystem_Changed; 45 w. renamed + = watcher_Renamed; 46} 47 Console. readLine (); 49} 50 51 # region [check whether the file is occupied] 52 // <summary> 53 // check whether the file is occupied 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}

 

There are still bugs.

The compiled exe can be attached and run directly.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.