Reputation: 43
I hit this error
The process cannot access because it is being used by another process.
The error show at var br = new BinaryReader(new FileStream(fileName, FileMode.Open));
I trying to do this because i want the byte to be dynamic.
foreach (string dir in dirs)
{
string fileName = Path.GetFileName(dir);
using (Stream source = System.IO.File.OpenRead(dir))
{
byte[] buffer;
var br = new BinaryReader(new FileStream(fileName, FileMode.Open));
buffer = br.ReadBytes((int)br.BaseStream.Length);
}
}
Upvotes: 1
Views: 1124
Reputation: 31239
Can't you just do this:
using (var source = new FileStream(dir, FileMode.Open))
{
byte[] buffer;
var br = new BinaryReader(source);
buffer = br.ReadBytes((int)br.BaseStream.Length);
}
Alternative you can do this:
File.ReadAllBytes(dir);
Upvotes: 1
Reputation: 14231
Here you open the file at first time:
System.IO.File.OpenRead(dir)
Here you are trying to open the same file second time:
new FileStream(fileName, FileMode.Open)
Of course that file is already occupied.
Rewrite the code as follows:
using (var source = File.OpenRead(dir))
using (var br = new BinaryReader(source))
{
byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
}
Upvotes: 1