Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create folder without read only attribute in C#

Tags:

c#

file

I am trying to create a folder from my app in 'c:\' folder(eg:c\), but this folder is always created with "read-Only" permission.

I have tried the below codes but still unable to 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 these methods are not working or just changing the attributes of file and not folder.

like image 625
Mohamed Mahir Avatar asked Jan 30 '14 13:01

Mohamed Mahir


1 Answers

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 wish 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);
like image 112
jakebower Avatar answered Sep 30 '22 12:09

jakebower