{"id":27315,"date":"2024-03-16T08:15:45","date_gmt":"2024-03-16T08:15:45","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/"},"modified":"2024-03-22T10:44:20","modified_gmt":"2024-03-22T10:44:20","slug":"how-can-the-filesystemwatcher-be-used-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/","title":{"rendered":"How can the FileSystemWatcher be used in C#?"},"content":{"rendered":"<p>The FileSystemWatcher class is used to monitor changes in the file system, such as creating, modifying, deleting, and renaming files or directories. Below are the basic steps of using the FileSystemWatcher class:<\/p>\n<ol>\n<li>Create a FileSystemWatcher object and specify the path to monitor.<\/li>\n<li>Specify the type of files to monitor or choose to monitor all files.<\/li>\n<li>Choose the types of changes to monitor (create, modify, delete, rename).<\/li>\n<li>Option: Specify if subdirectories should be monitored (optional).<\/li>\n<li>Register an event handler to handle file system change events.<\/li>\n<li>Start monitoring.<\/li>\n<\/ol>\n<p>Here is an example demonstrating how to use the FileSystemWatcher class to monitor file changes in a specified path.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">using<\/span> System;\r\n<span class=\"hljs-keyword\">using<\/span> System.IO;\r\n\r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\r\n{\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>()<\/span>\r\n    {\r\n        <span class=\"hljs-comment\">\/\/ \u521b\u5efaFileSystemWatcher\u5bf9\u8c61\u5e76\u8bbe\u7f6e\u8981\u76d1\u89c6\u7684\u8def\u5f84<\/span>\r\n        FileSystemWatcher watcher = <span class=\"hljs-keyword\">new<\/span> FileSystemWatcher();\r\n        watcher.Path = <span class=\"hljs-string\">\"C:\\\\Path\\\\To\\\\Directory\"<\/span>;\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u8981\u76d1\u89c6\u7684\u6587\u4ef6\u7c7b\u578b\u6216\u6240\u6709\u6587\u4ef6<\/span>\r\n        watcher.Filter = <span class=\"hljs-string\">\"*.txt\"<\/span>;\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u8981\u76d1\u89c6\u7684\u66f4\u6539\u7c7b\u578b<\/span>\r\n        watcher.NotifyFilter = NotifyFilters.LastWrite;\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u8bbe\u7f6e\u662f\u5426\u76d1\u89c6\u5b50\u76ee\u5f55<\/span>\r\n        watcher.IncludeSubdirectories = <span class=\"hljs-literal\">true<\/span>;\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u6ce8\u518c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/span>\r\n        watcher.Changed += OnChanged;\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u542f\u52a8\u76d1\u89c6<\/span>\r\n        watcher.EnableRaisingEvents = <span class=\"hljs-literal\">true<\/span>;\r\n\r\n        <span class=\"hljs-comment\">\/\/ \u7b49\u5f85\u7528\u6237\u8f93\u5165\u4ee5\u9000\u51fa\u7a0b\u5e8f<\/span>\r\n        Console.WriteLine(<span class=\"hljs-string\">\"Press enter to exit.\"<\/span>);\r\n        Console.ReadLine();\r\n    }\r\n\r\n    <span class=\"hljs-comment\">\/\/ \u6587\u4ef6\u66f4\u6539\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">OnChanged<\/span>(<span class=\"hljs-params\"><span class=\"hljs-built_in\">object<\/span> source, FileSystemEventArgs e<\/span>)<\/span>\r\n    {\r\n        <span class=\"hljs-comment\">\/\/ \u5904\u7406\u6587\u4ef6\u66f4\u6539\u4e8b\u4ef6<\/span>\r\n        Console.WriteLine(<span class=\"hljs-string\">$\"File <span class=\"hljs-subst\">{e.Name}<\/span> has been <span class=\"hljs-subst\">{e.ChangeType}<\/span>.\"<\/span>);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>In the example above, we created a FileSystemWatcher object to monitor changes to .txt files in the path &#8220;C:\\Path\\To\\Directory&#8221;. Then, we registered an OnChanged event handler to handle file change events. Finally, we started the monitoring process and printed information about file changes on the console. You can customize the path, file type, and event handler to meet your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The FileSystemWatcher class is used to monitor changes in the file system, such as creating, modifying, deleting, and renaming files or directories. Below are the basic steps of using the FileSystemWatcher class: Create a FileSystemWatcher object and specify the path to monitor. Specify the type of files to monitor or choose to monitor all files. [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-27315","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How can the FileSystemWatcher be used in C#? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can the FileSystemWatcher be used in C#?\" \/>\n<meta property=\"og:description\" content=\"The FileSystemWatcher class is used to monitor changes in the file system, such as creating, modifying, deleting, and renaming files or directories. Below are the basic steps of using the FileSystemWatcher class: Create a FileSystemWatcher object and specify the path to monitor. Specify the type of files to monitor or choose to monitor all files. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T08:15:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T10:44:20+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"How can the FileSystemWatcher be used in C#?\",\"datePublished\":\"2024-03-16T08:15:45+00:00\",\"dateModified\":\"2024-03-22T10:44:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\"},\"wordCount\":176,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\",\"name\":\"How can the FileSystemWatcher be used in C#? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T08:15:45+00:00\",\"dateModified\":\"2024-03-22T10:44:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How can the FileSystemWatcher be used in C#?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How can the FileSystemWatcher be used in C#? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/","og_locale":"en_US","og_type":"article","og_title":"How can the FileSystemWatcher be used in C#?","og_description":"The FileSystemWatcher class is used to monitor changes in the file system, such as creating, modifying, deleting, and renaming files or directories. Below are the basic steps of using the FileSystemWatcher class: Create a FileSystemWatcher object and specify the path to monitor. Specify the type of files to monitor or choose to monitor all files. [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T08:15:45+00:00","article_modified_time":"2024-03-22T10:44:20+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"How can the FileSystemWatcher be used in C#?","datePublished":"2024-03-16T08:15:45+00:00","dateModified":"2024-03-22T10:44:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/"},"wordCount":176,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/","name":"How can the FileSystemWatcher be used in C#? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T08:15:45+00:00","dateModified":"2024-03-22T10:44:20+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-can-the-filesystemwatcher-be-used-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How can the FileSystemWatcher be used in C#?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/27315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=27315"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/27315\/revisions"}],"predecessor-version":[{"id":61538,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/27315\/revisions\/61538"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=27315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=27315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=27315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}