In our C # WinForms application, we create PDF files and run Adobe Reader (or something like the default .pdf system handler) through the class Process. Since our PDF files can be large (approximately 200K), we process the event Exitedto then clear the temp file afterwards.
The system works as needed when the file is opened and then closed again. However, when the second file is opened (before closing Adobe Reader), the second process immediately exits (since Reader now uses its MDI privileges), and in our handler, Exitedour File.Delete call should fail because it is blocked by the now connected Adobe process. However, in Reader we get:
Error opening this document. This file was not found.
Unusually, if I delete the debugging point before deleting the file and allow it to try (and fail) to delete, the system will behave as expected!
I am sure that the file exists and is pretty positive that all file descriptors / streams to the file are closed before starting the process.
We run the following code:
var pdfProcess = new Process();
pdfProcess.StartInfo.FileName = tempFileName;
if (pdfProcess.StartInfo.Verbs.Contains("open", StringComparer.InvariantCultureIgnoreCase))
{
var verb = pdfProcess.StartInfo.Verbs.First(v => v.Equals("open", StringComparison.InvariantCultureIgnoreCase));
pdfProcess.StartInfo.Verb = verb;
}
pdfProcess.StartInfo.Arguments = "/N";
pdfProcess.SynchronizingObject = this;
pdfProcess.EnableRaisingEvents = true;
pdfProcess.Exited += new EventHandler(pdfProcess_Exited);
_pdfProcessDictionary.Add(pdfProcess, tempFileName);
pdfProcess.Start();
Note. We use _pdfProcessDictionaryto store references to Process objects so that they remain in scope so that the Exited event can be successfully raised.
Our clean / exit event has:
void pdfProcess_Exited(object sender, EventArgs e)
{
Debug.Assert(!InvokeRequired);
var p = sender as Process;
try
{
if (_pdfProcessDictionary.ContainsKey(p))
{
var tempFileName = _pdfProcessDictionary[p];
if (File.Exists(tempFileName))
{
File.Delete(tempFileName);
_pdfProcessDictionary.Remove(p);
}
CleanOtherFiles();
}
}
catch (IOException ex)
{
}
}
Possible solutions:
- Determine that the file is still open in another process.
- Detecting that the second process has not been completely completed, and that the file is opened in the first process instead