Skip to content

Instantly share code, notes, and snippets.

@rockarts
Created June 19, 2013 17:53
Show Gist options
  • Save rockarts/5816349 to your computer and use it in GitHub Desktop.
Save rockarts/5816349 to your computer and use it in GitHub Desktop.
Notice that I have passed the FileStream to the ProcessFiles. This creates a readlock on the file, making it impossible to let the file being opened by another program or thread. You use the FileStream to read the acquired file (eg StreamReader sr = new StreamReader(myFileStream) ).
string[] existingFiles = Directory.GetFiles(ProcessPath, "*.xml");
foreach (string fileName in existingFiles)
{
FileInfo fi = new FileInfo(fileName);
FileStream fs = null;
while (fs == null && fi.Exists)
{
try
{
fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (SecurityException se)
{
break;
}
catch
{
Thread.Sleep(100);
}
}
if (fs != null) ProcessFiles(fs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment