简体   繁体   中英

How to read an exe file when it is running in C#

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM