How to programmatically add a website (C #) in IIS 6.0?

We have a web application that is still installed under the "Default Web Site" in IIS. The client now wants us to install it on another website, and we want to do this from the installer. My question consists of 2 parts: A) How can I programmatically add another website next to the "default website"? B) We use the Windows installer - is there a way to run any code that I write for section A from the installer so that the installation is performed in a new location? It seems that overriding Install () is too late in the game ...

+4
source share
4 answers

we use js windows script to update our virtual directories between branches, I would suggest that you can use the same objects to create a website

var iisPath = "IIS://localhost/W3SVC/" + siteID; var site = GetObject(iisPath); 

Microsoft has a fairly extensive article on setting up IIS 6 programmatically. While your MSI can invoke a batch file, this article should help.

This article also has a complete script file that creates a website.

+3
source

Here is the C # code for programmatically creating the site:

 1 using System.DirectoryServices; 2 using System; 3 4 public class IISAdmin 5 { 6 public static int CreateWebsite(string webserver, string serverComment, string serverBindings, string homeDirectory) 7 { 8 DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc"); 9 10 //Create a website object array 11 object[] newsite = new object[]{serverComment, new object[]{serverBindings}, homeDirectory}; 12 13 //invoke IIsWebService.CreateNewSite 14 object websiteId = (object)w3svc.Invoke("CreateNewSite", newsite); 15 16 return (int)websiteId; 17 18 } 19 20 public static void Main(string[] args) 21 { 22 int a = CreateWebsite("localhost", "Testing.com", ":80:Testing.com", "C:\\inetpub\\wwwroot"); 23 Console.WriteLine("Created website with ID: " + a); 24 } 25 26 } 

link: http://www.it-notebook.org/iis/article/cs_create_website_iis6.htm

+2
source

If you are using WiX to create your MSI, see this question .

+1
source

Not very expert in MSI files, but you can add a site through scripting using adsutil.vbs. The wrong way to handle this is for your installer to delete files locally without creating a website, and then perform a later step using a script to create a website with a root in that location. Bonus points for making this optional for empty IIS administrators like me who hate installers who touch IIS in general.

0
source

Source: https://habr.com/ru/post/1286413/


All Articles