Using the system. directoryservices. directoryentry class provided by the. NET Framework, you can easily create an IIS Site and set the site to ASP. NET 2.0. Undoubtedly, this method is better than the previous one. Note the following code:
1. There is no path check. This is very simple;
2. There is no check for the port, which is not very complicated;
3. The scriptmapslist array is directly decomposed from strings for the sake of simplicity. Pay attention to your own applications;
4. You can add more IIS permissions as needed.
The code for this method is as follows:
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 ";
String [] scriptmapslist = system. Text. regularexpressions. RegEx. Split (scriptmaps, "\ r \ n ");
/// Note: No port check is performed here.
// Access flags
Const int md_access_read = 0x00000001; // allow read access.
Const int md_access_script = 0x00000200; // allow script execution.
String entpath = "IIS: // localhost/W3SVC ";
System. directoryservices. directoryentry rootentry = new system. directoryservices. directoryentry (entpath );
Int siteid = 1;
// Obtain the existing site ID
Foreach (system. directoryservices. directoryentry entry in rootentry. Children)
{
If (entry. schemaclassname = "iiswebserver ")
{
Int id = convert. toint32 (entry. Name );
If (ID> = siteid)
{
Siteid = ID + 1;
}
}
}
System. directoryservices. directoryentry newsiteentry = rootentry. Children. Add (siteid. tostring (), "iiswebserver ");
Newsiteentry. properties ["serverbindings"]. value = ": 8001 :";
Newsiteentry. properties ["servercomment"]. value = "mengxianhui2 ";
Newsiteentry. commitchanges ();
System. directoryservices. directoryentry vdirentry = newsiteentry. Children. Add ("root", "iiswebvirtualdir ");
Vdirentry. properties ["path"]. value = @ "C: \ mengxianhui ";
// System. Collections. arraylist scriptmapslist = new system. Collections. arraylist ();
// Scriptmapslist. Add (@ ". aspx, c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll, 1, get, Head, post, debug ");
// Scriptmapslist. Add (@ ". asax, c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll, 5, get, Head, post, debug ");
// Scriptmapslist. Add (@ ". SVC, c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll, 5, get, Head, post, debug ");
// Scriptmapslist. Add (@ ". msgx, c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll, 5, get, Head, post, debug ");
// Scriptmapslist. Add (@ ". config, c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll, 5, get, Head, post, debug ");
// Scriptmapslist. Add (@ ". ascx, c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll, 5, get, Head, post, debug ");
Vdirentry. properties ["scriptmaps"]. value = scriptmapslist;
Vdirentry. properties ["appfriendlyname"] [0] = "Meng xianhui's website ";
Vdirentry. properties ["appisolated"] [0] = 2;
Vdirentry. properties ["accessflags"] [0] = md_access_read | md_access_script;
Vdirentry. properties ["approot"] [0] = "lm/w3svc/" + siteid + "/root ";
Vdirentry. commitchanges ();
Newsiteentry. commitchanges ();
MessageBox. Show ("created! ");
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/net_lover/archive/2007/08/26/1759880.aspx