Hi,
I am working on winform applications
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
.....
}
am writing above code to Read DAT file but some times I am getting following exception
"The process cannot access the file 'C:\data\myfile.dat' because it is being used by another process."
Can you tel me how to solve this problem ?
Read dat file in winforms applications
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Nov 4, 2011, 5:03 AM EDT
1. You've opened the file elsewhere in your application but not closed it.
2. Another user, or application, has already opened the file for exclusive access?
Suthish NairRank #92 · 22.8K reputationPosted Nov 6, 2011, 5:12 AM EST
bool blnFileAlreayOpened = false;
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
blnFileAlreayOpened = true;
}
blnFileAlreayOpened = false;
}
catch (Exception)
{
blnFileAlreayOpened = true;
}
if (blnFileAlreayOpened == true)
{
// throw error
}