0

I want to delete Read-only Folder. I did like this

  //Remove Read-only for the Folder
File.SetAttributes(folderpath, File.GetAttributes(folderpath) & ~FileAttributes.ReadOnly);
//Delete Folder
      FileInfo myfileinf = new FileInfo(folderpath);
        myfileinf.Delete();

But i get this Error "Access to the path 'E:\Working Folder\RPEssential\RPEssential\ResourcePlus-PL\RDLReports\t' is denied".

Such
  • 285
  • 2
  • 4
  • 19
  • 2
    The user your ASP.NET Application Pool is running with must have the proper permissions to delete that folder. Hope this helps – juanreyesv Apr 23 '13 at 05:27
  • @juanreyesv But Admin had set the permission already – Such Apr 23 '13 at 05:32
  • Try manually removing the read only flag and execute the code. Whether it's working? – Ramesh Durai Apr 23 '13 at 05:36
  • 1
    @Sachin if you are trying to delete the entire folder you should use Directory.Delete [method](http://msdn.microsoft.com/en-au/library/fxeahc5f(v=vs.100).aspx) – juanreyesv Apr 23 '13 at 05:38
  • Check out this.http://stackoverflow.com/questions/1701457/directory-delete-doesnt-work-access-denied-error-but-under-windows-explorer-it – MahaSwetha Apr 23 '13 at 05:45
  • @juanreyesv yes I am trying to delete entire folder – Such Apr 23 '13 at 05:47
  • Ok @Sachin so try to use Directory.Delete() Method. There is a good example in this page http://msdn.microsoft.com/en-au/library/fxeahc5f(v=vs.100).aspx – juanreyesv Apr 23 '13 at 05:49

2 Answers2

2

As I commented earlier the problem is that you are trying to delete a folder as you were deleting a file.

You should use Directory.Delete method to delete a folder.

In the following link there is a good example on how to use it

http://msdn.microsoft.com/en-au/library/fxeahc5f(v=vs.100).aspx

public static void Main() 
{
    // Specify the directories you want to manipulate.
    string path = @"c:\MyDir";
    string subPath = @"c:\MyDir\temp";

    try 
    {
        // Determine whether the directory exists.
        if (!Directory.Exists(path)) 
        {
            // Create the directory.
            Directory.CreateDirectory(path);
        }


        if (!Directory.Exists(subPath)) 
        {
            // Create the directory.
            Directory.CreateDirectory(subPath);
        }

        // This will succeed because subdirectories are being deleted.
        Console.WriteLine("I am about to attempt to delete {0}", path);
        Directory.Delete(path, true);
        Console.WriteLine("The Delete operation was successful.");

    } 
    catch (Exception e) 
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    } 
    finally {}
}
juanreyesv
  • 853
  • 9
  • 22
0

There can be many reasons for a read only to be denied. Is the folder in use? Open in a console? Running an executable? All of these things you should check. Even if it has the permission, if the directory is in use, it won't allow you to delete it.