{"id":261,"date":"2012-08-24T13:55:15","date_gmt":"2012-08-24T13:55:15","guid":{"rendered":"b1a59b315fc9a3002ce38bbe070ec3f5"},"modified":"2012-08-24T13:55:15","modified_gmt":"2012-08-24T13:55:15","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/261","title":{"rendered":"\u89e3\u51b3fileSystemWatcher\u7684onChanged\u7684\u4e8b\u4ef6\u89e6\u53d1\u591a\u6b21\u7684\u95ee\u9898"},"content":{"rendered":"<div id=\"sina_keyword_ad_area2\" class=\"articalContent \">\n<div>\n<p>Q\uff1a<\/p>\n<blockquote>\n<p>FileSystemWatcher \u7684Changed\u4e8b\u4ef6\uff0c\u5728\u6211\u4fdd\u5b58\u67d0\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8be5\u4e8b\u4ef6\u53ef\u80fd\u4f1a\u88ab\u51fa\u53d1\u591a\u6b21\uff0c\u5982\u4f55\u4f7f\u4ed6\u89e6\u53d1\u4e00\u6b21\uff1f<\/p>\n<\/blockquote>\n<p>&nbsp;<wbr><\/p>\n<p>A:<\/p>\n<blockquote>\n<p>\u89e3\u51b3fileSystemWatcher\u7684onChanged\u7684\u4e8b\u4ef6\u89e6\u53d1\u591a\u6b21\u7684WorkAround\u65b9\u6cd5\u5c31\u662f\u5c1d\u8bd5\u5728\u76d1\u89c6\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u53d1\u751f\u53d8\u5316\u65f6\u901a\u77e5Framework, \u5177\u4f53\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\">My best guess is the FileWatcher fires the Create event once the file is opened for <\/pre>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\">write and then again when it completes. I've added a test for file existence in the <\/pre>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\">Event Handler and it does indeed FAIL on the first callback. The code below produced<\/pre>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\"> a series of debug prints showing every other callback has the OS reporting the file<\/pre>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\"> exists, i.e., 2,4,6,...\r\n\r\n\r\nnamespace MyExample\r\n{\r\n    class Program\r\n    {\r\n        private static FileSystemWatcher watcher;\r\n        private static string mailbox = @\"c:\\Mailbox\";\r\n        private static int callbacks = 0;\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            watcher = new FileSystemWatcher();\r\n            watcher.Path = mailbox;\r\n            watcher.NotifyFilter = NotifyFilters.FileName;\r\n            watcher.Created += new FileSystemEventHandler(OnChanged);\r\n            watcher.EnableRaisingEvents = true;\r\n        \r\n\r\n            Console.WriteLine(\"Press Enter to quit\\r\\n\");\r\n            Console.ReadLine();    \r\n        }\r\n\r\n\r\n\r\n        public static void OnChanged(object source, FileSystemEventArgs e)\r\n        {\r\n            try\r\n            {\r\n                watcher.EnableRaisingEvents = false;\r\n                callbacks++;\r\n                FileInfo objFileInfo = new FileInfo(e.FullPath);\r\n                if (!objFileInfo.Exists) return;   <\/pre>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\">\/\/ ignore the file open, wait for complete write\r\n                ProcessAllFiles(e.FullPath);\r\n\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                string msg = ex.Message;\r\n                \/\/ do something?\r\n            }\r\n            finally\r\n            {\r\n                watcher.EnableRaisingEvents = true;\r\n            }\r\n        }\r\n\r\n\r\n\r\n\r\n        public static void ProcessAllFiles(string FullPath)\r\n        {\r\n            \/\/ stub\r\n\r\n\t    string directory = FullPath.Substring(0, FullPath.LastIndexOf(@\"\\\"));\r\n            string filename = FullPath.Substring(FullPath.LastIndexOf(@\"\\\"));\r\n            Console.WriteLine(\"file: {0} Callback # {1} \", filename, callbacks);  \r\n        }<\/pre>\n<\/p>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<div>\u5173\u4e8e\u89e3\u51b3fileSystemWatcher\u7684onChanged\u7684\u4e8b\u4ef6\u89e6\u53d1\u591a\u6b21\u7684WorkAround\u65b9\u6cd5\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u8003\u4e0b\u8ff0\u6587\u7ae0\uff1ahttp:\/\/www.codeproject.com\/useritems\/FileWatcher.asp<\/div>\n","protected":false},"excerpt":{"rendered":"<div id=\"sina_keyword_ad_area2\" class=\"articalContent \">\n<div>\n<p>Q\uff1a<\/p>\n<p>FileSystemWatcher \u7684Changed\u4e8b\u4ef6\uff0c\u5728\u6211\u4fdd\u5b58\u67d0\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8be5\u4e8b\u4ef6\u53ef\u80fd\u4f1a\u88ab\u51fa\u53d1\u591a\u6b21\uff0c\u5982\u4f55\u4f7f\u4ed6\u89e6\u53d1\u4e00\u6b21\uff1f<\/p>\n<p>&nbsp;<wbr><\/p>\n<p>A:<\/p>\n<p>\u89e3\u51b3fileSystemWatcher\u7684onChanged\u7684\u4e8b\u4ef6\u89e6\u53d1\u591a\u6b21\u7684WorkAround\u65b9\u6cd5\u5c31\u662f\u5c1d\u8bd5\u5728\u76d1\u89c6\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u53d1\u751f\u53d8\u5316\u65f6\u901a\u77e5Framework, \u5177\u4f53\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/div>\n<\/div>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/261\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-article"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}