File.Delete Access to the path is denied

前端 未结 5 967
无人共我
无人共我 2021-01-18 05:26

My console application program is creating some runtime files while it is working so what I want to do is delete all of these files on the application startup. I have tried

5条回答
  •  天命终不由人
    2021-01-18 06:05

    Try using the Microsoft.VisualBasic.FileIO.FileSystem methods as it has a handy DeleteDirectory method, I had access troubles awhile ago and this was the fix for my problem.

    var directory = new DirectoryInfo(targetDir);
    if (directory.Exists)
    {
        Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(targetDir, Microsoft.VisualBasic.FileIO.DeleteDirectoryOption.DeleteAllContents);
    }
    

提交回复
热议问题