Opening a file that's in use by another process - c#

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

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.

Related

Is there a way I can allow a specific process to also read a file while FileStream is opened?

My program opens a file for reading purposes and does not let other programs access its contents. This is exactly what I want to do, except another specific process also needs to read the file. I do not manage this process, so that would be ideal if I could define it by its name or PID.
What I have done:
FileStream fileStream = new FileStream(FILE, FileMode.Open, FileAccess.Read, FileShare.None);
The problem with what I did, as previously stated, was it won't allow the other process to read from it. I know I could just have it close the FileStream, use File. Read, or change FileShare, but I only one my process and the other process to read the file.

Can I take the ownership of lock when trying to open and write a file through StreamWriter?

I use StreamWriter to write logs. I sometimes open this log file with Excel while my program is running.
I find that Excel lock this file so I get IOException when my program try to write logs.
Can I take out the lock which was set by other process?
I know I can lock the file while my program is running but it cause similar problem when I open it with Excel.
Note that I won't write anything with the other process.
using(var sw = new StreamWriter(
new FileStream(filename, FileMode.Append, FileAccess.Write), Encoding.GetEncoding("UTF-16")))
No, you can't "take ownership of lock on file" (unless other application is designed o allow such access).
The other application must be opening file with particular share mode to allow your to simultaneously access the file.
Note that most application don't open files in such mode due to problems keeping state consistent.
.Net way of specifying share mode - use desired value from FileShare enumeration when opening files. There are multiple samples and discussion about it on SO like C# multiple instances of program reading from same file.

Read a file that is locked (?) by a other process

I want to read the content of a file which is opened (and locked?) by a other process.
I tried it with File.ReadAllText() and with new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read)) but both methods trigger a IOException.
For example I can open the file with Notepad++ and the content is shown so I think it must be possible too with c#.
You need to use the FileStream constructor overload that takes a FileShare argument. And pass FileShare.ReadWrite. You can only open the file if you permit write access since the other program already acquired that right. Otherwise the reason that your attempts failed so far, they used FileShare.Read. Can't work, you cannot deny write access because the other program already got that.
Dealing with the program writing to the file while you are reading it is entirely up to you. Results can be quite random. Anything is possible, but in general for a log file you'll get a partially written last line that's trailing behind the actual output of the program, some of which is still in the program's file buffer. A buffer size of 4096 bytes is a common choice.

Read already opened file

Is there a way to read binary data from a read-only file? I have an Excel worksheet, which might be opened in Excel but I want to open it for read purposes only.
I tried to do it this way:
using (FileStream fileStream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
And I am getting
The process cannot access the file 'something.xlsx' because it is being used by another process.
Is there any way to achieve that?
Change this argument:
FileShare.Read
to this:
FileShare.ReadWrite
You are attempting to deny write access to the file, which is causing your issue as Excel already has it open for writing.
You can't open a file who are already open. Be careful to close your file after open it. And you should verify that you didn't use the file in another software (at the same time)...

How to open a StreamReader in ShareDenyWrite mode?

How do i open a StreamReader with FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE?
Same question, slightly expanded
How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can read the file?
How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can modify the file while i'm reading it?
How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can delete the file while i'm reading it?
Same question, slightly more expanded
In the .NET Framework class library there is a class called StreamReader. It is the only class designed to read "text", which is why it descends from the abstract base TextReader class. The TextReader/StreamReader allows you to specify the encoding used by the file you are trying to open, and can decode the file for you, returning Strings of text.
Once i've opened a file with the StreamReader:
var sr = new StreamReader(path);
The file is locked, with other processes unable to modify or delete the file. What i need is the equivalent of a FileStream class's FileShare enumeration:
None: 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.
Read": Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
Write: Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
ReadWrite:Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
Delete: Allows subsequent deleting of a file.
Except that, for obvious reasons, i cannot use a FileStream - have to use a StreamReader.
How can i open a StreamReader with FileShare.ReadWrite | FileShare.Delete?
StreamReader has a constructor that can take a stream. So instead of using the constructor that takes a string path, first create a FileStream with the options that you want, then pass that FileStream to the StreamReader constructor.
How can i open a StreamReader with FileShare.ReadWrite | FileShare.Delete ?
When you have solved the problem for a Stream, the Reader is easy:
var fs = new FileStream(fileName, FileMode.Open, FileShare.ReadWrite|FileShare.Delete);
var sr = new StreamReader(fs);
And of course that should be wrapped in a using() { } block.

Categories

Resources