C# System.IO.FileStream 文件正由另一进程使用,因此该进程无法访问该文件

文件正由另一进程使用,因此该进程无法访问该文件

原来是构造System.IO.FileStream时,使用的方法有问题

一开始是直接使用

System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open)

这个方法打开文件的时候是以只读共享的方式打开的,但若此文件已被一个拥有写权限的进程打开的话,就无法读取了,

因此需要使用

System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open,System.IO.FileAccess.Read,FileShare.ReadWrite);

设置文件共享方式为读写,FileShare.ReadWrite,这样的话,就可以打开了


1.jpg 10.png
1.png
1.png
网站制作-网站建设-公司网站-产品销售网站-产品展示网站-企业宣传网站-网店-网上商城-信息分类网站-QQ471226865 网站制作QQ471226865
### C# 中处理文件被占用导致的 `System.IO.IOException` 异常 当尝试访问在被其他进程使用文件时,可能会遇到 `System.IO.IOException` 异常。这种情况下,程序无法常读取或写入文件。 为了有效应对这种情况,在编写代码时可以采取以下措施: #### 尝试获取文件锁状态并重试逻辑 通过循环检测文件是否可用,并设置合理的等待时间间隔来实现自动重试机制[^3]。 ```csharp using System; using System.IO; public class FileHelper { public static string ReadFileWithRetry(string filePath, int maxRetries = 5, TimeSpan delayBetweenTries = default) { if (delayBetweenTries == default) { delayBetweenTries = TimeSpan.FromMilliseconds(100); } for (int i = 0; i < maxRetries; ++i) { try { using FileStream fs = new(filePath, FileMode.Open, FileAccess.Read, FileShare.None); using StreamReader sr = new(fs); return sr.ReadToEnd(); } catch (IOException ex) when ((ex.HResult & 0xFFFF) == 32 || (ex.HResult & 0xFFFF) == 33) { // ERROR_SHARING_VIOLATION or ERROR_LOCK_VIOLATION Thread.Sleep(delayBetweenTries); } } throw new IOException($"Failed to access file after {maxRetries} attempts."); } } ``` 上述代码展示了如何安全地打开文件进行读取操作的同时处理可能发生的共享冲突异常。如果捕获到特定类型的 I/O 错误,则会短暂休眠段时间后再做下次尝试直到达到最大重试次数为止;若最终仍未能成功则抛出新的异常通知调用方失败情况。 #### 使用异步方式减少阻塞风险 对于长时间运行的任务建议采用异步编程模型以提高应用程序响应速度和用户体验。下面是个简单的例子说明怎样利用 async/await 关键字完成同样的功能: ```csharp using System; using System.IO; using System.Threading.Tasks; public static class AsyncFileHelper { private const int MaxRetries = 5; private static readonly Random _randomizer = new(); public static async Task<string> TryReadAllTextAsync(string path) { Exception lastException = null!; for (var attemptCount = 0; attemptCount < MaxRetries; ++attemptCount) { try { await using Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); using var reader = new StreamReader(stream); return await reader.ReadToEndAsync().ConfigureAwait(false); } catch (IOException ioEx) { lastException ??= ioEx; // Add some jitter to avoid thundering herd problem. await Task.Delay(_randomizer.Next(100 * attemptCount)).ConfigureAwait(false); } } throw lastException; } } ``` 这段代码实现了更优雅的方式来处理因文件锁定而引发的操作系统级别的错误。它不仅支持多次重试还加入了随机延迟策略(jitter),从而降低了多个客户端同时请求相同资源造成竞争条件的可能性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值