Programmatically create a web site in IIS using C# and set port number

前端 未结 5 944
春和景丽
春和景丽 2020-11-28 04:16

We have been able to create a web site. We did this using the information in this link:

https://msdn.microsoft.com/en-us/library/ms525598.aspx

However, we wo

5条回答
  •  执念已碎
    2020-11-28 05:04

    I have gone though all answer here and also tested. Here is the most clean smarter version of answer for this question. However this still cant work on IIS 6.0. so IIS 8.0 or above is required.

    string domainName = "";
    string appPoolName = "";
    string webFiles = "C:\\Users\\John\\Desktop\\New Folder";
    if (IsWebsiteExists(domainName) == false)
    {
        ServerManager iisManager = new ServerManager();
        iisManager.Sites.Add(domainName, "http", "*:8080:", webFiles);
        iisManager.ApplicationDefaults.ApplicationPoolName = appPoolName;
        iisManager.CommitChanges();
    }
    else
    {
        Console.WriteLine("Name Exists already"); 
    }
    
    
    public static bool IsWebsiteExists(string strWebsitename)
    {
        ServerManager serverMgr = new ServerManager();
        Boolean flagset = false;
        SiteCollection sitecollection = serverMgr.Sites;
        flagset = sitecollection.Any(x => x.Name == strWebsitename);
        return flagset;
    }
    

提交回复
热议问题