I have an interface in my program to a process that creates a PDF file. After the PDF file is created, I want to rename it. But I am too quick because I try to rename it and it doesn't exist yet. So the PDF-creation routine must be returning success to me, but then asynchronously creating the file. That is fine if I can wait for it.
So my problem is I am using FileInfo and the first time I check it for .Exists the file doesn't. How do I check again? Can I refresh the FileInfo structure? Do I have to keep allocating a new one? I want to basically sleep and retry, but how do I get recent stats on the file after I sleep?
But if continue loops to again check theFile.Exists, it will always be false because it's static and has info from the first time.Code:FileInfo theFile = new FileInfo(outputFilePath + oldFileName); while (bWaitForFile && nbrAttempts < maxAttempts) { if (theFile.Exists) File.Move(outputFilePath + oldFileName, outputFilePath + newFileName); else { System.Threading.Thread.Sleep(5000); ++nbrAttempts; continue; } }




Reply With Quote