Search This Blog

Sunday, March 6, 2011

Deleteing ReadOnly Files from Directory


Normally the delete folder/sub folder API wont delete any read only files in the directory. The following code snippet will allow you to forcefully delete all the files in a folder.

.Net API => Directory.Delete(Folder name)..cant delete read only files.

For deleting Read only files:

   1:  string command = @"c:\Windows\System32\CMD.exe";   
   2:  string parameter = @"/F c:\temp\MyReadOnlyFile.txt";   
   3:  ProcessStartInfo startInfo = new ProcessStartInfo(command, parameter);   startInfo.CreateNoWindow = true;   
   4:  Process.Start(startInfo); 
For Directories use RMDIR command with /S & /Q arguments.

   1:  string command = @"c:\Windows\System32\CMD.exe";    
   2:  string parameter = @"/S /Q c:\temp\MyReadOnlyFolder";    
   3:  ProcessStartInfo startInfo = new ProcessStartInfo(command, parameter);    
   4:  startInfo.CreateNoWindow = true;    
   5:  Process.Start(startInfo);


2 comments:

  1. Haahaaa I think I have already introduced a simple mechanism to delete readonly files in the WFCommon FolderHelper. Interesting...

    ReplyDelete