Create IIS Website


/ Published in: C#
Save to your folder(s)

Properties that can be set are found here: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cde669f1-5714-4159-af95-f334251c8cbd.mspx?mfr=true


Copy this code and paste it in your HTML
  1. static void CreateSite(string metabasePath, string siteID, string siteName, string physicalPath, string poolname)
  2. {
  3. // metabasePath is of the form "IIS://<servername>/<service>"
  4. // for example "IIS://localhost/W3SVC"
  5. // siteID is of the form "<number>", for example "555"
  6. // siteName is of the form "<name>", for example, "My New Site"
  7. // physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
  8. Console.WriteLine("\nCreating site {0}/{1}, mapping the Root application to {2}:",
  9. metabasePath, siteID, physicalPath);
  10.  
  11. try
  12. {
  13. DirectoryEntry service = new DirectoryEntry(metabasePath);
  14. string className = service.SchemaClassName.ToString();
  15. if (className.EndsWith("Service"))
  16. {
  17. DirectoryEntries sites = service.Children;
  18. DirectoryEntry newSite = sites.Add(siteID, (className.Replace("Service", "Server")));
  19. newSite.Properties["ServerComment"][0] = siteName;
  20. newSite.Properties["ServerBindings"].Value = ":84:";
  21. newSite.Properties["DefaultDoc"].Value = "loginpage.aspx";
  22.  
  23. newSite.CommitChanges();
  24. DirectoryEntry newRoot;
  25. newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
  26. newRoot.Properties["Path"][0] = physicalPath;
  27. newRoot.Properties["AppFriendlyName"][0] = "websiteName";
  28. newRoot.Properties["AccessScript"][0] = true;
  29. newRoot.Properties["AccessExecute"][0] = true;
  30. newRoot.Properties["AppPoolId"].Value = poolname;
  31. newRoot.Invoke("AppCreate", 1);
  32. newRoot.CommitChanges();
  33.  
  34. //Start site
  35. newSite.Invoke("Start");
  36.  
  37. Console.WriteLine(" Done. Your site will not start until you set the ServerBindings or SecureBindings property.");
  38. }
  39. else
  40. Console.WriteLine(" Failed. A site can only be created in a service node.");
  41. }
  42. catch (Exception ex)
  43. {
  44. Console.WriteLine("Failed in CreateSite with the following exception: \n{0}", ex.Message);
  45. }
  46. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.