private void CreateWebSite()
{
try
{
string installPath = "C:\\Program Files\\MyWeb";
string IISVirtualDirectory = "MyWeb";
var root = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");
foreach (DirectoryEntry directoryEntry in root.Children)
{
if (directoryEntry.Name == IISVirtualDirectory)
{
try
{
root.Invoke("Delete", new[] { directoryEntry.SchemaClassName, IISVirtualDirectory });
root.CommitChanges();
}
catch (Exception)
{
}
}
}
DirectoryEntry de = root.Children.Add(IISVirtualDirectory, "IIsWebVirtualDir");
de.Properties["Path"][0] = installPath + @"\MyWebSite";
de.Invoke("AppCreate", true);
de.Properties["AppFriendlyName"][0] = IISVirtualDirectory;
//IIS下,将Framework自动对应到4.0版本。
Object[] mappings = (Object[])de.InvokeGet("ScriptMaps");
StringBuilder sb = new StringBuilder();
foreach (var a in mappings)
{
sb.Append(a + "\r\n");
}
ArrayList list = AddScriptArray();
de.CommitChanges();
}
catch
{
}
}
/// <summary>
/// IIS下,将Framework自动对应到4.0版本。
/// </summary>
/// <returns></returns>
private ArrayList AddScriptArray()
{
string specialFolder = "c:";
try
{
specialFolder = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2);
}
catch
{
}
ArrayList list = new ArrayList();
list.Add(".asp," + specialFolder + @"\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE");
list.Add(".cer," + specialFolder + @"\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE");
list.Add(".cdx," + specialFolder + @"\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE");
list.Add(".asa," + specialFolder + @"\WINDOWS\system32\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE");
list.Add(".idc," + specialFolder + @"\WINDOWS\system32\inetsrv\httpodbc.dll,5,OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE");
list.Add(".shtm," + specialFolder + @"\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST");
list.Add(".shtml," + specialFolder + @"\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST");
list.Add(".stm," + specialFolder + @"\WINDOWS\system32\inetsrv\ssinc.dll,5,GET,POST");
list.Add(".asax," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".ascx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".ashx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".asmx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".aspx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".axd," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".vsdisco," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".rem," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".soap," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".config," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".cs," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".csproj," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".vb," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".vbproj," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".webinfo," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".licx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".resx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".resources," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".master," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".skin," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".compiled," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".browser," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".mdb," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".jsl," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".vjsproj," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".sitemap," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".msgx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".ad," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".dd," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".ldd," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".sd," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".cd," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".adprototype," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".lddprototype," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".sdm," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".sdmDocument," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".ldb," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".mdf," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".ldf," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".java," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".exclude," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".refresh," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".xamlx," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG");
list.Add(".aspq," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".cshtm," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".cshtml," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5");
list.Add(".vbhtm," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".vbhtml," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG");
list.Add(".svc," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1");
list.Add(".xoml," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,1");
list.Add(".rules," + specialFolder + @"\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll,5");
return list;
}
转载自:http://www.cnblogs.com/takako_mu/archive/2012/01/11/2318803.html