Reputation: 1097
I am trying to read an exe file when it is running as follows:
FileStream fs = new FileStream(assemblyPath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
But an exception raises: The file cannot be accessed because it is occupied by another process.
However, I can copy this file using Windows Explorer. So it is possible to read this file. How can I read it in my program? Thanks!
Upvotes: 0
Views: 277
Reputation: 15951
Try with:
FileStream fs =
new FileStream(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
The FileShare.Read flag is the key, it controls the kind of access other FileStream objects can have to the same file.
Upvotes: 1