Use C # To create an IIS Site and set the. NET Framework version to ASP. NET 2.0

Source: Internet
Author: User

/// <Summary>
/// This method creates a site (of course, there is no problem in creating a virtual directory, similar to this method), and sets ASP. NET in IIS to 2.0
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, eventargs E)
{
// Site name and physical path
String websitename = "mengxianhui ";
String pathtoroot = @ "C:" mengxianhui ";
Directoryentry root = new directoryentry ("IIS: // localhost/W3SVC"); // find unused id value for new web site
Int siteid = 1;
// Obtain the existing site ID
Foreach (directoryentry entry in root. Children)
{
If (entry. schemaclassname = "iiswebserver ")
{
Int id = convert. toint32 (entry. Name );

If (ID >= siteid)
{< br> siteid = ID + 1;
}< BR >}

// use the configuration file to create a site. You must stop the original service so that data can be written smoothly.
label1.text = "the service is being stopped ...... ";
application. doevents ();
system. serviceprocess. servicecontroller mobservicecontroller3 = new system. serviceprocess. servicecontroller ("IISADMIN");
foreach (system. serviceprocess. servicecontroller dependentservice in mobservicecontroller3.dependentservices)
{< br> switch (dependentservice. status)
{< br> case servicecontrollerstatus. stopped:
break;

Case servicecontrollerstatus. stoppending:
Dependentservice. waitforstatus (servicecontrollerstatus. Stopped );
Break;

Default:
Dependentservice. Stop ();
Dependentservice. waitforstatus (servicecontrollerstatus. Stopped );
Break;
}

}
If (mobservicecontroller3.status! = Servicecontrollerstatus. Stopped)
{
Mobservicecontroller3.stop ();
Mobservicecontroller3.waitforstatus (servicecontrollerstatus. Stopped );
}
// Make sure the service is completely stopped
Label1.text = "Service stopped! ";
Application. doevents ();
String configpath = system. environment. systemdirectory + @ "" inetsrv "metabase. xml ";
System. xml. xmldocument Doc = new system. xml. xmldocument ();
Doc. Load (configpath );

System. xml. xmlnamespacemanager xnm = new system. xml. xmlnamespacemanager (Doc. nametable );
Xnm. addnamespace ("mxh", "urn: Microsoft-catalog: xml_metabase_v64_0 ");

// to identify the largest website, add a node to its end
string sitelocation = "/lm/w3svc/" + (siteid-1 );
system. XML. xmlnode lastwebserver = Doc. selectsinglenode ("/mxh: configuration/mxh: mbproperty/mxh: iiswebvirtualdir [@ location = '" + sitelocation + "/root']", xnm );
If (lastwebserver = NULL)
{< br> MessageBox. show ("there is no existing web server. ");
Doc = NULL;
return;
}

// locate the adminacl settings, which change every time -_-!
system. XML. xmlnode lastwebserverfilter = Doc. selectsinglenode ("/mxh: configuration/mxh: mbproperty/mxh: iisfilters [@ location = '" + sitelocation + "/filters']/@ adminacl", xnm );
If (lastwebserver = NULL)
{< br> MessageBox. show ("there is no existing web server filters. ");
Doc = NULL;
return;
}

String lastwebserverfilteradminacl = lastwebserverfilter. value;

// Create a new site
Label1.text = "Create a new site ...... ";
Application. doevents ();
String newsiteid = "/lm/w3svc/" + siteid. tostring ();
System. xml. xmlnode newwebserver = Doc. createnode (system. xml. xmlnodetype. element, "", "iiswebserver", "urn: Microsoft-catalog: xml_metabase_v64_0 ");
System. xml. xmlattribute newwebserverlocation = Doc. createattribute ("location ");
Newwebserverlocation. value = newsiteid;
Newwebserver. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("authflags ");
Newwebserverlocation. value = "0 ";
Newwebserver. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("serverautostart ");
Newwebserverlocation. value = "true ";
Newwebserver. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("serverbindings ");
Newwebserverlocation. value = ": 802 :";
Newwebserver. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("servercomment ");
Newwebserverlocation. value = websitename;
Newwebserver. Attributes. append (newwebserverlocation );
Lastwebserver. parentnode. insertafter (newwebserver, lastwebserver );

System. xml. xmlnode newwebserverfilter = Doc. createnode (system. xml. xmlnodetype. element, "", "iisfilters", "urn: Microsoft-catalog: xml_metabase_v64_0 ");
Newwebserverlocation = Doc. createattribute ("location ");
Newwebserverlocation. value = newsiteid + "/filters ";
Newwebserverfilter. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("adminacl ");
Newwebserverlocation. value = lastwebserverfilteradminacl;
Newwebserverfilter. Attributes. append (newwebserverlocation );

Newwebserver. parentnode. insertafter (newwebserverfilter, newwebserver );

System. xml. xmlnode iiswebvirtualdir = Doc. createnode (system. xml. xmlnodetype. element, "", "iiswebvirtualdir", "urn: Microsoft-catalog: xml_metabase_v64_0 ");
Newwebserverlocation = Doc. createattribute ("location ");
Newwebserverlocation. value = newsiteid + "/root ";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("accessflags ");
Newwebserverlocation. value = "accessread | accessscript ";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("appfriendlyname ");
Newwebserverlocation. value = "Default ApplicationProgram";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("appisolated ");
Newwebserverlocation. value = "2 ";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("approot ");
Newwebserverlocation. value = newsiteid + "/root ";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("authflags ");
Newwebserverlocation. value = "authanonymous | authntlm ";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

// Permissions can be combined as needed
Newwebserverlocation = Doc. createattribute ("dirbrowseflags ");
Newwebserverlocation. value = "dirbrowseshowdate | dirbrowseshowtime | dirbrowseshowsize | dirbrowseshowextension | dirbrowseshowlongdate | enabledefaultdoc ";
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverlocation = Doc. createattribute ("path ");
Newwebserverlocation. value = pathtoroot;
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

// For security, the following system folders must be obtained using the program method, such as system. environment. systemdirectory, and system. runtime. interopservices. runtimeenvironment. getruntimedirectory ()
// Write data directly during the simple Period
String scriptmaps = @ ". asp, C:" Windows "System32" inetsrv "ASP. dll, 5, get, Head, post, trace
. CER, C: "Windows" System32 "inetsrv" ASP. dll, 5, get, Head, post, trace
. CDX, C: "Windows" System32 "inetsrv" ASP. dll, 5, get, Head, post, trace
. Asa, C: "Windows" System32 "inetsrv" ASP. dll, 5, get, Head, post, trace
. IDC, C: "Windows" System32 "inetsrv" httpodbc. dll, 5, get, post
. Shtm, C: "Windows" System32 "inetsrv" ssinc. dll, 5, get, post
. Shtml, C: "Windows" System32 "inetsrv" ssinc. dll, 5, get, post
. Stm, C: "Windows" System32 "inetsrv" ssinc. dll, 5, get, post
. Asax, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Ascx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Ashx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Asmx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Aspx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Axd, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Vsdisco, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. REM, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Soap, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Config, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. CS, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Csproj, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. VB, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Vbproj, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Webinfo, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Licx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Resx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Resources, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Xoml, C: "Windows" Microsoft. NET "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Rules, C: "Windows" Microsoft. NET "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. Master, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Skin, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Compiled, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Browser, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Mdb, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Jsl, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Vjsproj, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Sitemap, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Msgx, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. AD, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. DD, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. LDD, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. SD, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. CD, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Adprototype, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Lddprototype, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. SDM, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Sdmdocument, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. LDB, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. SVC, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 1, get, Head, post, debug
. MDF, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. LDF, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Java, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Exclude, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug
. Refresh, C: "Windows" microsoft.net "Framework" v2.0.50727 "aspnet_isapi.dll, 5, get, Head, post, debug ";

Newwebserverlocation = Doc. createattribute ("scriptmaps ");
Newwebserverlocation. value = scriptmaps;
Iiswebvirtualdir. Attributes. append (newwebserverlocation );

Newwebserverfilter. parentnode. insertafter (iiswebvirtualdir, newwebserverfilter );
Doc. Save (configpath );
Doc = NULL;

label1.text = "site created! ";
mobservicecontroller3 = NULL;
mobservicecontroller3 = new system. serviceprocess. servicecontroller ("W3SVC");
mobservicecontroller3.start ();
mobservicecontroller3.waitforstatus (servicecontrollerstatus. running);
mobservicecontroller3 = NULL;
label1.text = "congratulations, all work is done! ";
MessageBox. Show (" congratulations, all work is done! ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.