Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.CreateDirectory access to path is denied?

enter image description here

I have server-client application, it's a file manager
my problem is when I go inside a folder which requires access control like system folders, it becomes to read-only, but I need to move/delete or create new folder, how can I get the permission to do that?

here's how I create a new folder at the server side

public void NewFolder(string path)
    {
        try
        {
            string name = @"\New Folder";
            string current = name;
            int i = 0;
            while (Directory.Exists(path + current))
            {
                i++;
                current = String.Format("{0} {1}", name, i);
            }
            Directory.CreateDirectory(path + current);
            Explore(path); //this line is to refresh the items in the client side after creating the new folder
        }
        catch (Exception e)
        {
            sendInfo(e.Message, "error");
        }
    }
like image 432
Murhaf Sousli Avatar asked Dec 04 '22 17:12

Murhaf Sousli


2 Answers

There are often directories on a drive that even a user with administrator privileges cannot access. A directory with a name like "HDDRecovery" is quite likely to be troublesome like this. Surely it contains sensitive data that helps the user recover from disk failure. Another directory that fits this category is "c:\system volume information", it contains restore point data.

An admin can change the permissions on folders like this. But of course that doesn't solve the real problem nor is it a wise thing to do. Your user can't and shouldn't. Be sure to write code that deals with permission problems like this, simply catch the IOExeption. Keep the user out of trouble by never showing a directory that has the Hidden or System attribute set. They are the "don't mess with me" attributes.

like image 183
Hans Passant Avatar answered Dec 10 '22 10:12

Hans Passant


If you want to remove directory read-only attribute use this: http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/cb75ea00-f9c1-41e5-ac8e-296c302827a4

If you want to access system folders you can run your program as local administrator.

like image 31
user1227804 Avatar answered Dec 10 '22 10:12

user1227804