CharsetDetector / UTF-unknown

Character set detector build in C# - .NET 5+, .NET Core 2+, .NET standard 1+ & .NET 4+
303 stars 46 forks source link

CharsetDetector file access issues #109

Closed Spiralis closed 4 years ago

Spiralis commented 4 years ago

The current CharsetDetector is ok if you have exclusive access to the file in question. If you are however analyzing files as they are in use by other processes, like with me, where I am analyzing log-files being written to by other processes, then it fails:

System.IO.IOException: 'The process cannot access the file '...\Application.log' because it is being used by another process.'

I see that the CharsetDetector uses this code to open the file:

using (FileStream fileStream = File.OpenRead(filePath))

I believe that File.OpenRead does not explicitly allow others to ReadWrite while itself is doing its own operations (if I have understood this right).

In my own program I extensively read the files myself, and I had the same issue myself - at first. I fixed it by adding some more information when opening the file, to make sure that access can be shared with others:

using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

I don't remember exactly where I got it from, but I think MSDN docs + StackOverflow were my sources on this.

I believe that this should be a non-intrusive simple fix.

Spiralis commented 4 years ago

I have confirmed that the PR works for my project.

rstm-sf commented 4 years ago

I don't remember exactly where I got it from, but I think MSDN docs + StackOverflow were my sources on this.

Yes, this is a good solution, thanks!