-6

There is one text file in hard drive which continuously writing by some application XYZ.

Now, from application ABC, I am trying to read the file, but I am getting error,

the process cannot access the file because it is being used by another process

Note - I don't have control over XYZ application, still is there any way to read the file from application ABC?

byte[] data = File.ReadAllBytes(File.FullName)
user584018
  • 10,186
  • 15
  • 74
  • 160
  • Isn't the error clear enough? The other process has that file locked, so nobody can read it or modify it. – Camilo Terevinto Dec 12 '17 at 09:54
  • then is there any solution – user584018 Dec 12 '17 at 09:54
  • This means the other application is currently accessing the file. You have to close the stream to the file from the other app first. – Bassem Dec 12 '17 at 09:54
  • If you want to find out which process is locking the file, consider taking a look at [this](https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net) post. – Ian H. Dec 12 '17 at 09:56
  • Other application `XYZ`, we don't have any control. Is there any other way that I can do something from application 'ABC` – user584018 Dec 12 '17 at 09:56
  • Afraid not - if the other process has opened the file in non-sharing mode, you simply won't be able to open it in any other process. – Matthew Watson Dec 12 '17 at 09:59
  • can we check this ? `if the other process has opened the file in non-sharing mode` – user584018 Dec 12 '17 at 10:00
  • Try this solution https://stackoverflow.com/questions/897796/how-do-i-open-an-already-opened-file-with-a-net-streamreader – Clinton Prakash Dec 12 '17 at 10:01
  • You might want to see if you can successfully open the file using `System.IO.File.Open` with the various sharing flags (e.g. `System.IO.FileShare.ReadWrite`). But if the other application is opening it exclusively, there's nothing you can do about it. – Dylan Nicholson Dec 12 '17 at 10:02
  • 1
    Try to read file with `using (var fs = new FileStream("path", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))` – Evk Dec 12 '17 at 10:06
  • (1) The FileShare setting applies to subsequent openings of the file, so it is the other process that must set it correctly. (2) `File.ReadAllBytes()` already opens the file in Read mode. If it doesn't work with `File.ReadAllBytes()` then unfortunately it won't work with `new FileStream()` either, no matter what parameters you use. – Matthew Watson Dec 12 '17 at 10:19
  • @MatthewWatson that's not true. ReadAllBytes specifies `FileShare.Read`. If file is already opened with `FileAccess.Write` or ReadWrite - `ReadAllBytes` will fail to read it. However, if you specify `FileShare.ReadWrite` - you will be able to read that file if another party specified compatible share mode (`FileShare.Read` or `ReadWrite`). – Evk Dec 12 '17 at 10:25
  • Thanks @Evk, You'r solution is working – user584018 Dec 12 '17 at 10:35
  • Dear All, why so many down vote? May be my question is duplicate, but not incorrect – user584018 Dec 12 '17 at 10:36
  • @Evk That's strange - see for example [this answer](https://stackoverflow.com/a/4689958/106159) – Matthew Watson Dec 12 '17 at 11:10
  • @MatthewWatson Seems to confirm my point. "And then it performs the opposite check - does your FileShare parameter match up with their FileAccess parameters?" FileShare in case of ReadAllBytes is FileShare.Read. It is not compatible with any FileAccess which includes Write. So, ReadAllBytes is not happy when any other process (not future, but already existing too) writes to the file. But FileShare.ReadWrite is compatible with FileAccess.Write. So, both FileShare and FileAccess should be compatible with those of other already existing. – Evk Dec 12 '17 at 11:12
  • @MatthewWatson you can also confirm with experiment. Start thread and create filestream with `FileAccess.Write` and `FileShare.ReadWrite` and slowly write something in it. Then from main thread try to read it with ReadAllBytes - that will fail. Then try to read with FileShare.ReadWrite - that will work. – Evk Dec 12 '17 at 11:14
  • @Evk OK, that makes sense. (I was thinking along the lines that asking for "Read sharing" access was less restrictive than asking for "ReadWrite", but it's actually the other way around) – Matthew Watson Dec 12 '17 at 11:19

1 Answers1

3

Access the file in read mode so that you will not get this error.
Try this solution
How do I open an already opened file with a .net StreamReader?

Clinton Prakash
  • 967
  • 9
  • 20