Manage IIS virtual directories and application pools using C#

18/06/2009

tags: , ,
6 comments

 Hi


We can manage IIS using c# code, it’s a good option to automate our modifications on the IIS servers.


A great and inspiring code by Robbe Morris for that can be found here.


Here’s in brief – how to manage the IIS:


using System.DirectoryServices; // of course …


How to connect IIS:
DirectoryEntry iisServer = new DirectoryEntry(“IIS://” + myServerName + “/W3SVC/1”);


 


How to manage virtual directories:


Get root of virtual directories (the web site):
DirectoryEntry folderRoot = iisServer.Children.Find(“Root”, “IIsWebVirtualDir”);


Locate a virtual dir under it:
DirectoryEntry myVirDir = folderRoot.Children.Find(myVirtualDirectoryName, “IIsWebVirtualDir”);


or create it:
 folderRoot.Children.Add(myVirtualDirectoryName, “IIsWebVirtualDir”);


Set the virtual directory path:
myVirDir.Properties[“Path”].Insert(0, realPath);


Set Windows authentication:
myVirDir.Properties[“AuthNTLM”][0] =true;


Set Anonymous access:
myVirDir.Properties[“AuthAnonymous”][0] = true;
myVirDir.Properties[“AnonymousUserName”][0] = Username;
myVirDir.Properties[“AnonymousUserPass”][0] = password;


Set default page:
myVirDir.Properties[“EnableDefaultDoc”][0] = true;
myVirDir.Properties[“DefaultDoc”].Value = strMyDefaultPage;


Commit changes after modifying:


 myVirDir.CommitChanges();
 folderRoot.CommitChanges();
 iisServer.CommitChanges();


How to manage application pools:  


Get Application Pools Root:
DirectoryEntry iisServerPools = new DirectoryEntry(“IIS://” + serverName + “/W3SVC/AppPools”);


Locate a pool:
DirectoryEntry applPoolEntry = iisServerPools.Children.Find(myApplPoolName,”IIsApplicationPool”);


In addition – how to set application pool identity (not in the link above):
applPoolEntry.Properties[“WAMUserName”][0] = username;
applPoolEntry.Properties[“WAMUserPass”][0] = password;
applPoolEntry.Properties[“AppPoolIdentityType”][0] = 3; ///0  for LocalSystem, 1 for  LocalService, 2  for NetworkService, 3 for SpecifiedUser


And do not forget to CommitChanges…


Dan


 



 




 


 


 

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

6 comments

  1. Richard16/02/2010 ב 03:20

    Great code..it helped with some stuff Iw as working on.

    I used this as the base for loading a list of App Pools into a ListBox..now I need a way to recycle an App Pool…any ideas?

    Thanks.

    P.S. I got as far as the .Invoke(….)

    Reply
    1. Ian12/07/2014 ב 02:36

      I assume by now you have moved on but for other looking for more documentation on recycling app pools there is a good article here.

      http://blogs.msdn.com/b/akshayns/archive/2007/07/20/how-to-change-application-pool-identity-programmatically.aspx

      Reply
  2. jhon18/12/2012 ב 13:16

    I have a project in asp.net with c #, I need to create an installer that you post in the iis.

    What should I do? What are the next steps?

    I need the installer:

    Create the virtual directory
    copy the project folder
    assign the user with permission

    Reply
  3. Les Ferguson18/02/2015 ב 00:55

    It was posted long ago and far away, but…
    Any suggestions for tracking down exception of type ‘System.Runtime.InteropServices.COMException’, when trying to instantiate a DirectoryEntry as shown here?

    Reply
    1. Les Ferguson18/02/2015 ב 04:50

      OK, so IIS7 does not implement this interface by default, but it can be enabled by adding in IIS 6 Management Compatibility.

      Reply