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

今天做一个小程序监控目录并序列化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));
                     }
                 }

 提示:如果是以windows 服务的形式访问网络文件时,不能通知映射盘符的方式,必须是以IP或者hostname方式,比如不能是Z:\\abc\,必须为\\192.168.1.2\abc\

转载于:https://www.cnblogs.com/TandyChan/p/4819672.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值