Friday, April 29, 2022

C# Check if file exists after delete (issue, no solution)

 I seem to have a lot of issues managing files with C#. I have this check to make sure the file is gone after doing a delete. Manually checking, the file is gone but the app check finds the file (but does not find it after I manually check). Maybe timing issue?

fi.Delete does not always delete

Although EXE files will throw an error, there are some scenarios for non-EXE files to not throw an error. In this case, the process will continue as if the delete was executed successfully.

I have not identified why this is the case. More interestingly on the Windows end, I can "sometimes" manually delete the file with Windows explorer. Sometimes I cannot. No clue why or how or when. And when it cannot, I can rename it then delete it with Windows explorer. Why? No clue.

Delete with Process

So, I have a check if the file still exists. If it exists, I execute a command line delete: 
cmd.exe /C del [filepath]

I use Process to execute this with a WaitForExit()


Check if file still exists

Even after all this, the "fi.Exists" still returns true even when the file does not exist anymore. Does it exist when at the time of execution, I don't know. But if I debug it, it does not exist and executes the proper path. There may be some time for the process to execute??


Summary

So somehow after attempting to delete twice, the fi.exists still returns true for some reason in full execution mode but not in debug mode. Why? No clue. 

No comments:

Post a Comment