Create a Web Site in IIS Using C#

Ever wondered how to interface with IIS using .NET? If all your apps/clients are running IIS 6.0, it is drop dead easy, just enable the XML metabase and parse it to your heart's content. However, if you need to support older clients, you will probably end up using DirectoryServices. However, since DirectoryServices is fairly generic, you might have difficulty figuring out what exactly you need to do. In case you get stuck, here is a little reference block of code that will create a new web site with the supplied name and root directory:

public class SetupUtility

{

public SetupUtility()

{

}

public int CreateWebSite(string webSiteName, string pathToRoot)

{

return CreateWebSite(webSiteName, pathToRoot, false);

}

public int CreateWebSite(string webSiteName, string pathToRoot, bool createDir)

{

DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");

// Find unused ID value for new web site

int siteID = 1;

foreach(DirectoryEntry e in root.Children)

{

if(e.SchemaClassName == "IIsWebServer")

{

int ID = Convert.ToInt32(e.Name);

if(ID >= siteID)

{

siteID = ID+1;

}

}

}

// Create web site

DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);

site.Invoke("Put", "ServerComment", webSiteName);

site.Invoke("Put", "KeyType", "IIsWebServer");

site.Invoke("Put", "ServerBindings", ":80:");

site.Invoke("Put", "ServerState", 2);

site.Invoke("Put", "FrontPageWeb", 1);

site.Invoke("Put", "DefaultDoc", "Default.aspx");

site.Invoke("Put", "SecureBindings", ":443:");

site.Invoke("Put", "ServerAutoStart", 1);

site.Invoke("Put", "ServerSize", 1);

site.Invoke("SetInfo");

// Create application virtual directory

DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");

siteVDir.Properties["AppIsolated"][0] = 2;

siteVDir.Properties["Path"][0] = pathToRoot;

siteVDir.Properties["AccessFlags"][0] = 513;

siteVDir.Properties["FrontPageWeb"][0] = 1;

siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root";

siteVDir.Properties["AppFriendlyName"][0] = "Root";

siteVDir.CommitChanges();

site.CommitChanges();

return siteID;

}

}

Published Wednesday, September 17, 2003 4:06 AM by Jesse Ezell

Comments

# re: Create a Web Site in IIS Using C#

Wednesday, September 17, 2003 1:54 AM by Jonathan Cogley
I wonder if this is how the NAnt mkiisdir task does it?
http://nantcontrib.sourceforge.net/help/tasks/mkiisdirtask.html

Hmmm, it looks like it uses the DirectoryServices approach that you mention.
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/nantcontrib/NAntContrib/src/Tasks/IISTasks.cs?rev=1.4&content-type=text/vnd.viewcvs-markup

# re: Create a Web Site in IIS Using C#

Sunday, October 26, 2003 4:58 PM by Matt Miller
Great sample!
I used it as the basis for a program I wrote recently, and I was getting an error message saying application unavailable whenever I tried to connect to the web site.

As soon as I removed this line;
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root"; the site was created correctly and I no longer got that error. All of the web sites being created by my program are asp.net sites so I am uncertain whether this occurs in all web sites or only asp.net sites.

I checked the AppRoot property for several sites created manually in IIS and in each case the AppRoot property either didn't exist or was set to "".

# re: Create a Web Site in IIS Using C#

Sunday, December 28, 2003 1:57 AM by Mike
Jesse you are a legend, I have been trying to figure this one out for ages. Thanks. Mike

# re: Create a Web Site in IIS Using C#

Friday, January 16, 2004 4:01 PM by ASP scripts will not run in IIS 6.0
Just in case anyone has any problems getting asp scripts after using the above code.

Simply replace this line:
siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/"+siteID+"/Root";

with this line:
siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/"+siteID+"/Root";

that one "/" makes all the difference. :)

-Matt Brooks

# re: Create a Web Site in IIS Using C#

Monday, January 26, 2004 4:30 PM by Panayot Belchev
Thank you great reference. It is almost impossible to find examples on the internet on this topic. I implemented this in VB.NET as well, and added some more functionality like Bandwidth and LogFile setup.

I have uploaded the code here:

http://www.panayot.com/articles/iis.htm

It is tested on IIS 6.0

By the way it is a little dangerous to edit the MetaBase.xml file directly because if a malformated xml is saved to this file, this could crash the IIS server and force it to revert to an older backup of the metabase file if such exists.

Panayot

# re: Create a Web Site in IIS Using C#

Monday, January 26, 2004 4:49 PM by Panayot Belchev
I will add explanation and a link for the VB.NET code to be downloaded in a while.

# re: Create a Web Site in IIS Using C#

Tuesday, March 30, 2004 10:15 PM by Loreal
thanks,but how about using in IIS5.0?
soon i'll try.

# re: Create a Web Site in IIS Using C#

Tuesday, March 30, 2004 11:14 PM by Jesse Ezell
This works perfectly fine in IIS 5.0.

# re: Create a Web Site in IIS Using C#

Wednesday, April 14, 2004 2:29 AM by William Bartholomew
Something to be aware of in IIS 6.0 is that the siteIDs are no longer sequential, they are random, so just adding 1 to the siteID does not necessarily mean you will get a unique siteID. A better approach would be generating a random number (as the IIS Manager appears to), checking if it exists, and if not using it, if so repeat.

# re: Create a Web Site in IIS Using C#

Wednesday, April 21, 2004 8:47 AM by Found wrong!!!
Compiler Error Message: CS0234: The type or namespace name 'DirectoryServices' does not exist in the class or namespace 'System' (are you missing an assembly reference?)

what's the question?

# re: Create a Web Site in IIS Using C#

Wednesday, April 21, 2004 4:28 PM by wil
You need to add DirectoryServices.dll as a reference

# re: Create a Web Site in IIS Using C#

Wednesday, April 21, 2004 4:36 PM by wil
I'm getting Access denied on when running the code. Are there special permissions I need to set?

I copied the code verbatim, and called it with
SetupUtility su = new SetupUtility ();

su.CreateWebSite("test", "c:\inetpub\www\test", true);

Exception details System.Runtime.InteropServices.COMException: Access is denied

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

# re: Create a Web Site in IIS Using C#

Monday, May 03, 2004 11:53 PM by Bart Fibrich
Thanks for this.. Helped me a lot.

If you want the website to be attached to the DefaultApplicationPool you need to do something like.

DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
siteVDir.Properties["Path"][0] = pathToRoot;
siteVDir.Properties["AccessScript"][0] = true;
siteVDir.CommitChanges();
siteVDir.Properties["AppFriendlyName"][0] = "Default Application";
siteVDir.Invoke("AppCreate3", new object[] {2, "DefaultAppPool", true});
siteVDir.CommitChanges();

Not sure why it needs the double CommitChanges but its what I had to do to get it working for the ROOT
virtual directory. Works without the double commit for others. Strange!

# re: Create a Web Site in IIS Using C#

Monday, May 10, 2004 2:04 AM by eric
Same problem as will

site.Invoke("SetInfo");

causes an access denied.

running IIS6 pls help

# re: Create a Web Site in IIS Using C#

Monday, May 10, 2004 7:12 AM by Panayot Belchev
Eric,

IIS is using the default IUSR_ComputerName user for anonymous access. This user is a member of guests account so windows will not allow this account to touch active directory.
You can do two things:
1. open the website (or virtual directory) properties in IIS Manager, go to security, "Authentication and access control", click edit and change the default user to a more powerful user like Administrator. This means of course that whatever code is executed from this website, will have admin privileges, so be careful.

2. the other alternative is to impersonate your code with another user and then revert back to the default iis user. I have put an example here: http://www.panayot.com/articles/access-denied-iis.aspx

# re: Create a Web Site in IIS Using C#

Monday, May 10, 2004 7:44 AM by Panayot Belchev
Oh I have forgotten one important thing concerning method 1. above. You need to have these two lines in web.config:
<identity impersonate="true" />
<authentication mode="Windows" />

# re: Create a Web Site in IIS Using C#

Monday, May 10, 2004 2:27 PM by Eric
Gracias :)
you rock

# re: Create a Web Site in IIS Using C#

Monday, May 10, 2004 2:44 PM by Eric
Just curious. Is there any hard limits on how many IIS websites that can reside on a single IIS server?

I have never broken 20 sites, but a new software piece I am developing could create up to 1000 sites.

I couldn't imagine opening a enterprise manager with that many databases. wonder how iis will manage it.

# re: Create a Web Site in IIS Using C#

Tuesday, May 11, 2004 4:49 AM by Dieter
I try to figure out how to create Accounts on the same IIS but for different virtual Sites on that IIS.
Anyone any idea?

I did it manualy and compared the entrys, but could not fine any diff...

# re: Create a Web Site in IIS Using C#

Tuesday, May 11, 2004 4:49 AM by Dieter
I try to figure out how to create Accounts on the same IIS but for different virtual Sites on that IIS.
Anyone any idea?

I did it manualy and compared the entrys, but could not find any diff...

# re: Create a Web Site in IIS Using C#

Tuesday, May 11, 2004 2:37 PM by Chris Hood
How would you add more than one server binding. As i have to bind www.name.com and name.com to the site.

# re: Create a Web Site in IIS Using C#

Wednesday, May 26, 2004 2:57 PM by Sean P. Thomas
Has anyone come across a way to start/stop a specific web site in IIS through C#?

...And if so how?

# re: Create a Web Site in IIS Using C#

Wednesday, May 26, 2004 4:56 PM by Ole Lytjohan
_root = new DirectoryEntry("IIS://"+_serverName+"/W3SVC/1");
_root.Password = _password;
_root.Invoke("stop"); // and start

# IIS/ADSI/Microsoft.Net

Friday, May 28, 2004 11:10 PM by TrackBack

# re: Create a Web Site in IIS Using C#

Saturday, May 29, 2004 9:28 AM by Aftab
HI, i m trying to add host headers using above mentioned code as my bases, but so far i have not been able to figure which property respresent host headers, any clue?

# re: Create a Web Site in IIS Using C#

Monday, May 31, 2004 12:27 AM by Reddy
Hi ,

I am try to create a virtual folder with remote
physical path using C# .It is crating but vitual
folder has red mark and error. Pls give me replay .

mail id is grnr02@yahoo.com

# re: Create a Web Site in IIS Using C#

Monday, May 31, 2004 3:16 PM by Randy
I'm in the process of setting up a hosted web server and I need to know how to set up a subdomain programmatically "subdomain.domain.com" in asp.net/vb.net

# re: Create a Web Site in IIS Using C#

Monday, June 07, 2004 8:42 AM by Roberto
Hi, thank for your code.
Is there a way to set the IP Address for the web site?
I need to have several web site on the same 80 port so have I to set a unique IP address for each one or could I have different ways?
Thank you

# re: Create a Web Site in IIS Using C#

Saturday, June 12, 2004 2:54 AM by Z.J.Chen
then HOW TO delete a website? and where could i find the Invoke's "methodName"? I searched msdn,and cann't find the "methodName"(such as "Create","Put" etc.)?
my email:czj@6688.com. expecting your help!

# re: Create a Web Site in IIS Using C#

Monday, June 14, 2004 6:04 PM by GH McAdams
Does anyone know how to change things like Anonymous Access and default documents?

# re: Create a Web Site in IIS Using C#

Wednesday, June 16, 2004 12:47 PM by afterburn
IIS limits are around 1000, SQL is also the same but limit on tables per-db is 256.

# re: Create a Web Site in IIS Using C#

Wednesday, June 16, 2004 4:34 PM by afterburn
download IIS 6 resource kit. It contains metabase explorer which will provide you with valuable information on the structure....

Also a good idea to interate through the properties collection.

# re: Create a Web Site in IIS Using C#

Thursday, June 17, 2004 11:12 PM by Sakda
I followed your code and I can create a virtual directory properly in IIS. However, it didn't set the virtual folder as an application. I still have to go into IIS and create one manually by hitting button 'create' in 'home directory' of that particular virtual directory that I just created. Please let me know how could I accomplish this? Thank you in advance.

# re: Create a Web Site in IIS Using C#

Friday, June 18, 2004 8:17 AM by Jan
Wow. Great Reference. One question. I have several websites on my iis. now I want to get one of those to make some changes om it. How do I get a specific site in IIS when I don't know its ID?

# re: Create a Web Site in IIS Using C#

Tuesday, June 29, 2004 10:59 AM by Miguel Rodriguez
I have tried to add SSL with client authentication using this approach to a virtual directory. As soon as the changes are commited, they are visible from the IIS configuration console, but I need to manually click the APPLY button to activate the changes so that a user gets prompted to select a certificate to access the virtual directory. How can I do this "apply" action programmatically? Thanks in advance.

# re: Create a Web Site in IIS Using C#

Tuesday, July 20, 2004 11:04 AM by Nikita
First of all the documentation about IIS ADSI is here:

Use MSDN Contents chapters -> Web Development -> Server Technologies -> Internet Information Services (IIS) -> SDK Documentation -> Internet Information Services -> Reference -> IIS Administration Reference -> IIS ADSI Provider Reference -> IIS ADSI objects

Or you can try direct link in IE (MSDN online):
http://msdn.microsoft.com/library/en-us/iissdk/iis/ref_prog_iaoref.asp?frame=true

or just search in MSDN: "IIS ADSI"


Now for guys above...

Site deleting:

DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
root.Invoke("Delete", "IIsWebServer", iWebSiteID);


Site Stop/Start/Pause/Continue

DirectoryEntry oWebSite = new DirectoryEntry("IIS://localhost/W3SVC/"+iWebSiteID);
oWebSite.Invoke("Stop"); // or Start, Pause etc


# re: Create a Web Site in IIS Using C#

Tuesday, July 27, 2004 8:28 AM by wenbo
then HOW TO get state a website?

# re: Create a Web Site in IIS Using C#

Wednesday, July 28, 2004 9:08 AM by pooya
How to creat an IP Base website ?

# re: Create a Web Site in IIS Using C#

Saturday, July 31, 2004 11:01 AM by puya
i just get confused with PathToRootmso please
tell me what is it"
is it the same localpath????

# re: Create a Web Site in IIS Using C#

Saturday, July 31, 2004 11:02 AM by hello all
what is Pathtoroot,and how i can get the id
of site which ahas been added before??

# re: Create a Web Site in IIS Using C#

Sunday, August 01, 2004 5:22 AM by bpq
How to creat an IP Base website ?

# At the peak of my frustration comes clarity....

Thursday, October 21, 2004 12:23 PM by TrackBack

# [转载]Create a Web Site in IIS Using C#

Monday, August 14, 2006 10:06 PM by Klesh Wong

CreateaWebSiteinIISUsingC# http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspxEv...

# [转载]Create a Web Site in IIS Using C#

Monday, August 14, 2006 10:11 PM by Klesh Wong

CreateaWebSiteinIISUsingC# http://weblogs.asp.net/jezell/archive/2003/09/17/27869.aspxEv...

# re: Create a Web Site in IIS Using C#

Tuesday, November 07, 2006 10:26 PM by Vidit

can anybody tell me on how to set the ASP.Net version for a website through c#. By default the version is 1.1 and i want to set it to 2.0 through c#. Is this possible?

# re: Create a Web Site in IIS Using C#

Tuesday, January 16, 2007 12:27 AM by df

Found more help in this single blog than anywhere else.

I had to convert the code to VB though. Works like a charm.

I need a bit of help with this line. Instead of creating a site i would like to retrieve the information through this code. I think it is neat and clean.

DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID)

# re: Create a Web Site in IIS Using C#

Saturday, March 17, 2007 10:14 PM by Noemaol

[URL=http://popasite.info/free-porn-no-email/index.html]free porn no credit no email[/URL]|<a href=http://popasite.info/free-porn-no-email/index.html>free porn no credit no email</a>[URL=http://popasite.info/free-homemade-porn-video/index.html]free homemade porn video[/URL]|<a href=http://popasite.info/free-homemade-porn-video/index.html>free homemade porn video</a>[URL=http://popasite.info/free-homemade-porn-movie/index.html]amateur free homemade movie porn video[/URL]|<a href=http://popasite.info/free-homemade-porn-movie/index.html>amateur free homemade movie porn video</a>[URL=http://popasite.info/free-homemade-porn/index.html]free homemade movie porn xxx[/URL]|<a href=http://popasite.info/free-homemade-porn/index.html>free homemade movie porn xxx</a>[URL=http://popasite.info/free-japanese-porn-movie/index.html]download free japanese movie porn[/URL]|<a href=http://popasite.info/free-japanese-porn-movie/index.html>download free japanese movie porn</a>[URL=http://popasite.info/free-porn-movie-clip/index.html]free xxx porn movie clip[/URL]|<a href=http://popasite.info/free-porn-movie-clip/index.html>free xxx porn movie clip</a>[URL=http://popasite.info/free-amateur-porn-movie/index.html]amateur porn sex free movie[/URL]|<a href=http://popasite.info/free-amateur-porn-movie/index.html>amateur porn sex free movie</a>[URL=http://popasite.info/free-teen-porn-movie/index.html]free young teen porn movie[/URL]|<a href=http://popasite.info/free-teen-porn-movie/index.html>free young teen porn movie</a>[URL=http://popasite.info/teen-blow-job/index.html]teen blow job clip[/URL]|<a href=http://popasite.info/teen-blow-job/index.html>teen blow job clip</a>

# re: Create a Web Site in IIS Using C#

Friday, April 13, 2007 5:35 AM by dasjstair

just created the class according to my own needs i can set up almost everything except the creation of new application pools and including it to the site plus some ssl configurations... but thats just about it.. im researching more this one really help me... thanks jesse

# re: Create a Web Site in IIS Using C#

Thursday, August 09, 2007 5:36 AM by adi

Hi

I need to stop/start a website. I'm using IIS 5.1.

How can this be performed with AD?

Thanks.

# re: Create a Web Site in IIS Using C#

Monday, September 24, 2007 4:57 PM by bhopalite

Pl tell me the steps to create a subdomain using C#.NET

# re: Create a Web Site in IIS Using C#

Wednesday, January 23, 2008 12:46 PM by Rami

Hi,

I'm using oWebSite.Invoke("Start") to start a web site.

It works fine from visual studio in debug mode, but when I complie the code and put it in another web site I get an exception on the Invok ...

anyone encountered this ?

thanks

rami

# re: Create a Web Site in IIS Using C#

Sunday, January 27, 2008 5:02 AM by breezback

Hey,

Very interesting, I need to do quite the same but the server is Windows Server 2003 without active directory.

Is Hacking MetaBase xml the only way?

Any documentations?

Please reply also to ilan_levy@msn.com

# re: Create a Web Site in IIS Using C#

Wednesday, February 13, 2008 4:36 AM by Darren Loh

Hi, I've tried running the above code sample, but it did not reflect as what i'm expecting.

could it be the permission settings? Do i need to impersonate the administrator user?

# re: Create a Web Site in IIS Using C#

Wednesday, March 12, 2008 1:36 PM by ProTeuS

Hi,

Is there anyone who as a complete library with different functions or know where to find one?

I only find a few different functions not a more complete list.

Anyone? =)

Thanks!

# make money online

Sunday, March 30, 2008 9:26 PM by make money online

Hong Kong Broadband Network (HKBN) has officially launched a staggering 1Gb service- yes, that’ s a whopping 500 times faster than the 2Mb service that’ s available to most people in the UK. The price for this lightning fast package? Just 120/ month,

# re: Create a Web Site in IIS Using C#

Tuesday, May 06, 2008 1:08 PM by ctl00$main$ctl08$ctl02$ctl02$ctl02$tbname

emma guillen Ozzie <a href= ozzie-guillen.barerube.cn >guillen juice Ozzie</a> [url=ozzie-guillen.barerube.cn]guillen juice Ozzie[/url]

# ASP.NET VB.NET DNS and IIS Site Creation Program | FreeLance Home Jobs

Pingback from  ASP.NET VB.NET DNS and IIS Site Creation Program | FreeLance Home Jobs

# re: Create a Web Site in IIS Using C#

Saturday, January 24, 2009 6:05 AM by bhushan

Thanks a lot this really work....

http://www.nasbar.org

# re: Create a Web Site in IIS Using C#

Sunday, March 01, 2009 2:15 AM by Albina-an

<a href= http://adultdatingssfinder.com >singles</a>

# re: Create a Web Site in IIS Using C#

Sunday, March 01, 2009 2:15 AM by ellaela-ir

<a href= http://adultchatsfinder.com >chat online</a>

# re: Create a Web Site in IIS Using C#

Tuesday, February 15, 2011 5:14 AM by lcs

can anybody tell me on how to set the ASP.Net version for a website through c#. By default the version is 1.1 and i want to set it to 2.0 through c#. Is this possible?

# re: Create a Web Site in IIS Using C#

Tuesday, April 12, 2011 1:01 AM by football

Hello Everyone! I like watching BBC Football online.

# re: Create a Web Site in IIS Using C#

Wednesday, January 11, 2012 4:56 PM by camarop

<a href=http://2yd.net/1ji>fat burning furnace</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)