Create folder without read-only attribute in C #

I am trying to create a folder from my application in the c: \ folder (for example: c \), but this folder is always created with read-only permission.

I tried the codes below, but could not change the attributes. Please help me.

Method 1

var di = new DirectoryInfo(temppath);
File.SetAttributes(temppath, FileAttributes.Normal);
File.SetAttributes(temppath, FileAttributes.Archive); */

Method 2

di.Attributes = di.Attributes | ~FileAttributes.ReadOnly;
File.SetAttributes(temppath, File.GetAttributes(temppath) & ~FileAttributes.ReadOnly);

Method 3

foreach (string fileName in System.IO.Directory.GetFiles(temppath))
{
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);

    fileInfo.Attributes |= System.IO.FileAttributes.ReadOnly;
    // or
    fileInfo.IsReadOnly = false;
}

all of these methods do not work or just change the attributes of the file, not the folder.

+4
source share
1 answer

To create a directory:

DirectoryInfo di = Directory.CreateDirectory(path);

From MSDN: http://msdn.microsoft.com/en-us/library/54a0at6s%28v=vs.110%29.aspx

If you want to explicitly set access controls:

DirectorySecurity securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));

DirectoryInfo di = Directory.CreateDirectory(@"C:\destination\NewDirectory", securityRules);
+5
source

Source: https://habr.com/ru/post/1524503/


All Articles