码迷,mamicode.com
首页 > 其他好文 > 详细

FileSystemWatcher 读取文件时出现被占用的解决方法

时间:2015-09-18 18:14:21      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

今天做一个小程序监控目录并序列化XML时遇到第一次正常,第二个新文件加入时出现文件被占用的错误,记录下解决方法。

 1 FileSystemWatcher watcher = new FileSystemWatcher{
 2                 Path = path,
 3                 Filter = filter,
 4                 EnableRaisingEvents = true,
 5                 IncludeSubdirectories = true,
 6                 NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName |
 7                     NotifyFilters.FileName | NotifyFilters.LastAccess
 8                     | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size
 9             };
10             watcher.Created += OnCreated;
11 
12 private static void OnCreated(object source, FileSystemEventArgs e)
13         {
14             Console.WriteLine("Got a new file " + e.Name);
15 
16                 #region === 读取文件内容 ===
17                 string xmlStr = string.Empty;
18                 while (true)
19                 {
20                     try
21                     {
22                         using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
23                         {
24                             if (stream != null)
25                                 break;
26                         }
27                         System.Threading.Thread.Sleep(500);
28                     }
29                     catch (Exception ex)
30                     {
31                         Console.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.Name, ex.Message));
32                     }
33                 }
34 
35                 using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
36                 {
37                     using (StreamReader sr = new StreamReader(fs, Encoding.Default))
38                     {
39                         xmlStr = sr.ReadToEnd();
40                     }
41                 }
42                 #endregion
43         }

主要原因是文件还在写入过程中,还被占用,所以不能读取,以下代码为核心解决方法:

while (true)
                 {
                     try
                     {
                         using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                         {
                             if (stream != null)
                                 break;
                         }
                         System.Threading.Thread.Sleep(500);
                     }
                     catch (Exception ex)
                     {
                         Console.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.Name, ex.Message));
                     }
                 }

 

FileSystemWatcher 读取文件时出现被占用的解决方法

标签:

原文地址:http://www.cnblogs.com/TandyChan/p/4819672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!