The application should create a file in the directory, do something in the directory and then delete the file. For example, the source code is below:
File.Create("textfile.txt"); // Do something here File.Delete("textfile.txt");
If βsomethingβ is a process that requires a very short amount of time, File.Delete will raise an IOException (the file is being used by another process). According to another SO post: It is not possible to delete a directory using Directory.Delete (path, true) by calling Thread.Sleep (0) so that the previous process ends. However, even with
File.Create("textfile.txt"); // Do something here Thread.Sleep(0); File.Delete("textfile.txt");
nevertheless, the same IOException will be thrown.
The solution I got is a while loop that tries to delete the file again before it is deleted. But I wonder if Theres is the best solution.
Jim
source share