I have this problem in my application:
- Step 1 - create a file (xml) and put some content in it
- Step 2 - a third-party application will open the file and receive information from the file taken in step 1.
- Step 3 - delete the file again.
The first question I have is regarding this part of the code:
XmlDocument xmlDoc = new XmlDocument(); DataSet ds = //use a method to put in the data xmlDoc.LoadXml(ds.GetXml()); xmlDoc.Save("Filename"); // ... Process.Start(startInfo);
Is my assumption correct that the last line is executed only when it is already done? That way, I can be 100% sure that all the data is in xml before it tries to run it correctly?
The second part, where I get the error message, is here:
Process.Start(startInfo); File.Delete("Filename");
What is happening now is that the file is already deleted before a third-party program has read it in its memory.
Is there a way to verify that the file is no longer in use, or to make some kind of stable way to wait?
I already found a way to use Thread.Sleep(TimeInMiliSec); but I think this is the wrong way to do this (more like a workaround)?
source share