-2

I've looked at several solutions to reading a file that's already in use by another process, but none of them seem to work for me.

The file I'm trying to read is an XML file that contains configuration settings that I need to extract.

Here's what I have tried:

using (var stream = File.Open("\\\\2008r2\\c$\\ProgramData\\location\\siteConfig.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = new StreamReader(stream))
{
    // Actions you perform on the reader.
    while (!reader.EndOfStream)
    {
        Console.WriteLine(reader.ReadLine());
    }
}

This seems to work for just about everyone else, I don't know what I'm doing wrong! Is my file locked in a different manner and cannot be accessed even to read?

Help much appreciated!

Dave

4

1 回答 1

1

根据您的评论,原始进程已使用FileShare.None. 来自MSDN

拒绝共享当前文件。任何打开文件的请求(通过这个进程或另一个进程)都会失败,直到文件被关闭。

FileShare原始进程上有一个独占锁,因此除非枚举更改None或文件关闭,否则您将无法从中读取。

于 2014-11-08T19:55:43.687 回答