Created
June 19, 2013 17:53
-
-
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) ).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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