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\XM LFiles\492172.X ML' because
it is being used by another process
This is when doing:
CheckFileBeingU sed(xmlFile); // below - should give me up to 15
seconds to finish writing the file
fs = new FileStream(xmlF ile, FileMode.Open, System.IO.FileA ccess.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 CheckFileBeingU sed(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.FileA ccess.Read, FileShare.None) ;
inUse = false;
break;
}
catch (System.IO.IOEx ception exp)
{
inUse = true;
System.Threadin g.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\XM LFiles\492172.X ML' because
it is being used by another process
This is when doing:
CheckFileBeingU sed(xmlFile); // below - should give me up to 15
seconds to finish writing the file
fs = new FileStream(xmlF ile, FileMode.Open, System.IO.FileA ccess.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 CheckFileBeingU sed(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.FileA ccess.Read, FileShare.None) ;
inUse = false;
break;
}
catch (System.IO.IOEx ception exp)
{
inUse = true;
System.Threadin g.Thread.Sleep( 3000); // Wait 3
seconds and try again
}
}
return inUse;
}
*************** *************** *************** *
Thanks,
Tom
Comment