The old program manages IIS6 and cannot be used after the server updates iis7.
Introduction http://dflying.cnblogs.com/archive/2006/04/17/377276.html to this address found online
However, the above Code cannot be generated.
Servermanager iismanager = new servermanager ();
Iismanager. sites. Add ("newsite", "HTTP", "*: 8080:", "d: \ mysite ");
Iismanager. Update ();
No update method is displayed.
After querying msdn, find the solution and debug it successfully.
/// <Summary>
/// Iis7 create a website
/// </Summary>
/// <Param name = "name"> website name </param>
/// <Param name = "Port"> port </param>
/// <Param name = "Domain"> bind a domain name </param>
Public void createsitebyname (string name, string port, string domain)
{
String Path = @ "C: \ WEB \" + name + "Site ";
// Validate the site name
Char [] invalid = sitecollection. invalidsitenamecharacters ();
If (name. indexofany (invalid)>-1)
{
Console. writeline ("invalid site name: {0}", name );
}
// Create the content directory if it doesn't exist.
If (! System. Io. Directory. exists (PATH ))
{
System. Io. Directory. createdirectory (PATH );
}
// Create a new site using the new directory and update the config
Servermanager manager = new servermanager ();
Try
{// Add this site.
Site hrsite = manager. sites. Add (name, "HTTP", "*:" + port + ":" + domain, PATH );
Hrsite. serverautostart = true;
Manager. commitchanges ();
Console. writeline ("Site" + name + "added to applicationhost. config file .");
}
Catch
{
}
}
When the program is released, it is displayed that it has no permissions. You must add the network service user permission to c: \ windows \ system32 \ inetsrv \ config.