T
tshad
I am trying to get access to a file that may still being written because the
file is so large (7-10MB).
I get an error:
The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML' because
it is being used by another process
This is when doing:
CheckFileBeingUsed(xmlFile); // below - should give me up to 15
seconds to finish writing the file
fs = new FileStream(xmlFile, FileMode.Open, System.IO.FileAccess.Read);
I tried the following routine which should get an error if still in use. It
should run for 15 seconds (5 * 3), but even though the file is being
accessed (which I know because the FileStream error happens after the call),
it jumps out after the first call and never does go to the catch.
Why is this?
**********************************************
public bool CheckFileBeingUsed(string fileName)
{
// Check to see if file is in use. If very large it may be.
bool inUse = false;
for (int i = 0; i < 5; i++)
{
try
{
System.IO.File.Open(fileName, FileMode.Open,
System.IO.FileAccess.Read, FileShare.None);
inUse = false;
break;
}
catch (System.IO.IOException exp)
{
inUse = true;
System.Threading.Thread.Sleep(3000); // Wait 3
seconds and try again
}
}
return inUse;
}
**********************************************
Thanks,
Tom
file is so large (7-10MB).
I get an error:
The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML' because
it is being used by another process
This is when doing:
CheckFileBeingUsed(xmlFile); // below - should give me up to 15
seconds to finish writing the file
fs = new FileStream(xmlFile, FileMode.Open, System.IO.FileAccess.Read);
I tried the following routine which should get an error if still in use. It
should run for 15 seconds (5 * 3), but even though the file is being
accessed (which I know because the FileStream error happens after the call),
it jumps out after the first call and never does go to the catch.
Why is this?
**********************************************
public bool CheckFileBeingUsed(string fileName)
{
// Check to see if file is in use. If very large it may be.
bool inUse = false;
for (int i = 0; i < 5; i++)
{
try
{
System.IO.File.Open(fileName, FileMode.Open,
System.IO.FileAccess.Read, FileShare.None);
inUse = false;
break;
}
catch (System.IO.IOException exp)
{
inUse = true;
System.Threading.Thread.Sleep(3000); // Wait 3
seconds and try again
}
}
return inUse;
}
**********************************************
Thanks,
Tom