tags:

views:

30

answers:

1

How to set permissions on IIS6.0 virtual directory like write/read etc by C# code and script (By command line)?

+1  A: 

If you mean NTFS permissions then take a look at the following code:

/*
 * Set Modify permission on D:\MyWebSite and all children
*/

string path = @"D:\MyWebSite";
string userID = "BOB";

FileSystemRights rights = FileSystemRights.Modify;
InheritanceFlags inheritanceflags = 
          InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

DirectorySecurity acls = Directory.GetAccessControl(path);
FileSystemAccessRule acl =
   new FileSystemAccessRule(
          new NTAccount(userID),
          rights,
          inheritanceFlags,
          PropagationFlags.None,
          AccessControlType.Allow);
acls.AddAccessRule(acl);
Directory.SetAccessControl(path, acls);

For more info refer to:

DirectoryInfo.SetAccessControl Method (MSDN)

DirectorySecurity Class (MSDN)

FileSystemAccessRule Class (MSDN)

You could also use icacls.exe:

icacls.exe d:\MyWebSite /grant bob:(CI)(OI)M

Kev
Thanks for reply. I got my answer.
Sandy
@Sandy - it's courteous to upvote or mark as correct an answer that helped you out, otherwise the kind folks on StackOverflow will feel less inclined to assist you in the future.
Kev
@Kev can u tell me how to do upvote?
Sandy
@sandy - look to the top left of the answer, there's a number with up and down arrows and a tick. Click the up arrow and click the tick if you think the person helped you. The more you accept/upvote the more people will be inclined to help you.You also get a wee rep bump for accepting answers as well. People who consistently ask questions but not accept answers tend to be ignored eventually. I'd also recommend registering with the site properly or you may loose your rep and questions/answers if you clear your browser cookies.
Kev
@sandy - To demonstrate, I just upvoted your question because I thought it was a good question.
Kev