12

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 this:

public static void Empty(string targetDir)
{
    var directory = new DirectoryInfo(targetDir);
    if (!directory.Exists) return;
    foreach (var file in directory.GetFiles()) file.Delete();
    foreach (var subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}

...just to look for all the files/folders in the given path (which is in a subdirectory in the program execution path) then delete them. However, I get the following exception:

Access to the path 'file' is denied.

I tried to run the program as administrator with no luck; However, I want a solution that works without using administrator privileges.

Notes :

  1. The file is not running in another application.
  2. The file is not in a protected folder.
  3. The file can be deleted manually with no problems and that's why i am here.
Roman Ratskey
  • 5,101
  • 8
  • 44
  • 67

5 Answers5

12

I got this error and found that it was because my test files were readonly. Changed this and I can now use fileinfo to delete them no worries.

majjam
  • 1,286
  • 2
  • 15
  • 32
7
if (File.Exists(filePath))
{
    File.SetAttributes(filePath, FileAttributes.Normal);
    File.Delete(filePath);
}
roderickprince
  • 231
  • 1
  • 4
  • 10
  • 1
    Though this might answer the question, please also add a short explanation what your code does and why it solves the initial problem. – user1438038 Aug 10 '17 at 07:04
4

You say that the files are not open in another application, but it must be open within your application:

//Create some directories to delete
Directory.CreateDirectory("C:/Temp/DeleteMe");
Directory.CreateDirectory("C:/Temp/DeleteMe/DeleteMe");
File.Create("C:/Temp/DeleteMe/DeleteMeFile");//FileStream still open!!

//Delete the files
var directory = new DirectoryInfo("C:/Temp/DeleteMe");
if (!directory.Exists) return;
foreach (FileInfo file in directory.GetFiles())
{
    file.Delete();
}
foreach (DirectoryInfo dir in directory.GetDirectories())
{
    dir.Delete(true);
}

Make sure you dispose the file stream when you create the file

//Create some directories to delete
Directory.CreateDirectory("C:/Temp/DeleteMe");
Directory.CreateDirectory("C:/Temp/DeleteMe/DeleteMe");
using (File.Create("C:/Temp/DeleteMe/DeleteMeFile")) { }

//Delete the files
var directory = new DirectoryInfo("C:/Temp/DeleteMe");
if (!directory.Exists) return;
foreach (FileInfo file in directory.GetFiles())
{
    file.Delete();
}
foreach (DirectoryInfo dir in directory.GetDirectories())
{
    dir.Delete(true);
}
EdmundYeung99
  • 2,461
  • 4
  • 27
  • 43
3

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);
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
2

Using Windows API MoveFileEx might be a potential solution with a parameter MOVEFILE_DELAY_UNTIL_REBOOT to remove the file only after reboot.

Please check http://msdn.microsoft.com/en-us/library/aa365240%28v=vs.85%29.aspx.

  • Eventually the easiest way it would be to call native code, from C#. A quick search shows a nice example that will certainly cheer you up: http://stackoverflow.com/questions/6077869/movefile-function-in-c-sharp-delete-file-after-reboot-c-sharp –  Jul 14 '13 at 18:22