user2227986
user2227986

Reputation:

Can't create a folder in my logical drive - UnauthorizedAccessException: Access denied

System.Security.AccessControl.DirectorySecurity ds = new System.Security.AccessControl.DirectorySecurity();
                string useraccountForDirectoryCreate = System.Environment.UserDomainName;
                ds.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(useraccountForDirectoryCreate,
                System.Security.AccessControl.FileSystemRights.FullControl,
                        System.Security.AccessControl.InheritanceFlags.ContainerInherit |
                        System.Security.AccessControl.InheritanceFlags.ObjectInherit,
                        System.Security.AccessControl.PropagationFlags.None,
                        System.Security.AccessControl.AccessControlType.Allow));
                Directory.CreateDirectory(folder.Text,ds);
                Directory.CreateDirectory(folder.Text + "\\timetables",ds);

I'm trying to create a folder in my logical drive But I keep getting an exception: UnauthorizedAccessException Error : Access Denied I even run as administrator still the same results

Upvotes: 1

Views: 510

Answers (1)

phillip
phillip

Reputation: 2738

I re-wrote your code and tested it on my machine. This code works for me; however, so did your code so you must not have write permission to Program Files.

You should use Path.Combine(...) instead of manually appending your strings.

var ttg = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "TimeTableGenerator");
Directory.CreateDirectory(ttg);

Upvotes: 2

Related Questions