Reputation: 91
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.
Upvotes: 5
Views: 8823
Reputation: 151
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);
Upvotes: 5