正在访问共享文件? [英] Accessing a shared file?

查看:190
本文介绍了正在访问共享文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Windows共享文件夹的UNC路径读取文件正文,并得到以下异常:The process cannot access the file '\\<someIP>\logs\LogFiles\W3SVC1\u_ex141017.log' because it is being used by another process.
但是,此文件并没有真正被任何进程锁定.我可以使用文本编辑器等从PC上查看它.

I'm trying to read a file body from a Windows shared folder by it's UNC path, and getting this exception: The process cannot access the file '\\<someIP>\logs\LogFiles\W3SVC1\u_ex141017.log' because it is being used by another process.
However, this file isn't really locked by any process. I can view it from my PC using a text editor, etc.

我正在使用以下代码读取文件:

I'm using this code to read the file:

var logFile = File.ReadAllText(logPath);

var logFile = (string)null;
using (var fileStream = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.Delete))
{
    using (var reader = new StreamReader(fileStream))
    {
        logFile = reader.ReadToEnd();
    }
}

(均失败)

在文件没有被任何进程真正锁定的情况下,为什么会发生此异常的任何想法?

Any ideas why this exception might happen, when the file isn't really locked by any process?

推荐答案

尝试将FileShare.Delete更改为FileShare.ReadWrite.这将允许其他应用程序同时读取和写入文件.换句话说

Try changing FileShare.Delete to FileShare.ReadWrite. This will allow the file to be read and written by other applications simultaneously. In other words

var logFile = (string)null;
using (var fileStream = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var reader = new StreamReader(fileStream))
    {
        logFile = reader.ReadToEnd();
    }
}

这篇关于正在访问共享文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆