1

In my C# forms application, I try to download the data in a directory of my SFTP Server. The data should be stored in a folder which I want to create in "MyDocuments". When the folder is created, I receive an Renci error "failure" because the folder is "read-only".

I tried many ways to create a folder, but I in most ways I used I either got an error, that I don't have the permission to create a folder, or I got an empty file instead of a folder. Right now I got a folder, but unluckily it is read only.

String localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\MyNewFolder\\";

if (Directory.Exists(localPath))
{
    Console.WriteLine("Folder already exists");
}

if (!Directory.Exists(localPath))
{
    Directory.CreateDirectory(localPath);

    DirectoryInfo directory = new DirectoryInfo(localPath);
    DirectorySecurity security = directory.GetAccessControl();
}

I expect the folder not to be read only, so that I can safe data in it using my programm. Anyone knows why my code still creates a read only one?

Hammad Sajid
  • 312
  • 1
  • 3
  • 14
jonas_adam
  • 31
  • 4
  • 1
    Use a Window Explorer on Client and manually try to add from client a text file to the Network Folder. The windows credential may not allow you to write to the folder. Using a Windows Explorer will verify if you have the permissions to write to the folder. – jdweng Apr 30 '19 at 10:44
  • Check that `Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)` isn't returning null or "". Also if the directory already exists as readonly your code will not make any changes (obviously) so it will remain readonly. – Thomas N Apr 30 '19 at 10:47
  • I always delete the folder before I start my application, so I know that the folder is created as read only always.. – jonas_adam Apr 30 '19 at 10:50
  • You could try explicitly removing readonly like in this post: https://stackoverflow.com/a/2316534/4824531 – Thomas N Apr 30 '19 at 11:04
  • @ThomasN I already tried this one. Even with that method it creates a folder with the read only attribute – jonas_adam Apr 30 '19 at 11:07
  • Typically `Directory.CreateDirectory` does not create readonly folders. Is the app running under your own user credentials? Maybe permissions are inherited from parent folder. Can you manually create a folder in that parent directory? – derpirscher Apr 30 '19 at 11:26

1 Answers1

1

I believe you have to set the following using the DirectorySecurity object:

        DirectorySecurity securityRules = new DirectorySecurity();
        securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\Account", FileSystemRights.FullControl, AccessControlType.Allow));

Then you can create the directory using the following:

DirectoryInfo di = Directory.CreateDirectory(@"directoryToCreatePath", securityRules);

EDITED:

Once you've created the directory using Directory.CreateDirectory(), you can then apply the following to the folder. This will allow the user you've specified to have FullControl of the folder. You can check the permissions for that user via Properties > Security

DirectoryInfo directory = new DirectoryInfo("C:\\CreatedFolder");

DirectorySecurity security = directory.GetAccessControl();

security.AddAccessRule(new FileSystemAccessRule(@"USERNAME",
                                FileSystemRights.FullControl,
                                AccessControlType.Allow));

directory.SetAccessControl(security);