public static bool WaitForFile(string fullPath)
{
int numTries = 0;
while (true)
{
++numTries;
try
{
// Attempt to open the file exclusively.
using (FileStream fs = new FileStream(fullPath,
FileMode.Open, FileAccess.ReadWrite,
FileShare.None, 100))
{
fs.ReadByte();
// If we got this far the file is ready
break;
}
}
catch (Exception ex)
{
if (numTries > 10)
{
return false;
}
// Wait for the lock to be released
System.Threading.Thread.Sleep(300);
Console.WriteLine("Waiting : "+ex.Message);
}
}
return true;
}