-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

David C
  • 664
  • 1
  • 8
  • 21
  • Take a look at the MSDN documentation [MSDN File.Open Method](http://msdn.microsoft.com/en-us/library/y973b725(v=vs.110).aspx) – MethodMan Nov 08 '14 at 16:00
  • Are you getting a File in Use exception or another error message? – keyboardP Nov 08 '14 at 16:02
  • I'm getting an IOException, The process cannot access the file '\\2008r2\c$\ProgramData\location\siteConfig.xml' because it is being used by another process. I've read the MSDN for File Open but I can't figure it out, or perhaps the file is locked like I said? – David C Nov 08 '14 at 17:31
  • So I've had a look at the source code of the process that has the file open and they've used `FileShare.None`, does this mean that there is no way to read the file whatsoever? – David C Nov 08 '14 at 18:04

1 Answers1

1

From your comment, the original process has opened the file with FileShare.None. From MSDN:

Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.

The original process has an exclusive lock on it so you won't be able to read from it unless the FileShare enumeration is changed from None or the file is closed.

keyboardP
  • 68,824
  • 13
  • 156
  • 205