/// <summary> /// 创建網站或虚拟目录 /// </summary> /// <param name="WebSite">服务器站点名称(localhost)</param> /// <param name="VDirName">虚拟目录名称</param> /// <param name="Path">實際路徑</param> /// <param name="RootDir">true=網站;false=虛擬目錄</param> /// <param name="iAuth">设置目录的安全性,0不允许匿名访问,1为允许,2基本身份验证,3允许匿名+基本身份验证,4整合Windows驗證,5允许匿名+整合Windows驗證...更多請查閱MSDN</param> /// <param name="webSiteNum">1</param> /// <param name="serverName">一般為localhost</param> /// <returns></returns> public bool CreateWebSite(string WebSite, string VDirName, string Path, bool RootDir, int iAuth, int webSiteNum, string serverName) { try { System.DirectoryServices.DirectoryEntry IISSchema; System.DirectoryServices.DirectoryEntry IISAdmin; System.DirectoryServices.DirectoryEntry VDir; bool IISUnderNT; // // 确定IIS版本 // IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated"); if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN") { IISUnderNT = true; } else { IISUnderNT = false; } IISSchema.Dispose(); // // Get the admin object // 获得管理权限 // IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/W3SVC/" + webSiteNum + "/Root"); // // If we're not creating a root directory // 如果我们不能创建一个根目录 // if (!RootDir) { // // If the virtual directory already exists then delete it // 如果虚拟目录已经存在则删除 // foreach (System.DirectoryServices.DirectoryEntry v in IISAdmin.Children) { if (v.Name == VDirName) { // Delete the specified virtual directory if it already exists try { IISAdmin.Invoke("Delete", new string[] { v.SchemaClassName, VDirName }); IISAdmin.CommitChanges(); } catch (Exception ex) { throw ex; } } } } // // Create the virtual directory // 创建一个虚拟目录 // if (!RootDir) { VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir"); } else { VDir = IISAdmin; } // // Make it a web application // 创建一个web应用 // if (IISUnderNT) { VDir.Invoke("AppCreate", false); } else { VDir.Invoke("AppCreate", true); } // // Setup the VDir // 安装虚拟目录 //AppFriendlyName,propertyName,, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript,, true, false, false, true VDir.Properties["AppFriendlyName"][0] = VDirName; //应用程序名称 VDir.Properties["AccessRead"][0] = true; //设置读取权限 VDir.Properties["AccessExecute"][0] = false; VDir.Properties["AccessWrite"][0] = false; VDir.Properties["AccessScript"][0] = true; //执行权限[純腳本] //VDir.Properties["AuthNTLM"][0] = chkAuth; VDir.Properties["EnableDefaultDoc"][0] = true; VDir.Properties["EnableDirBrowsing"][0] = false; VDir.Properties["DefaultDoc"][0] = "Default.aspx,Index.aspx,Index.asp"; //设置默认文档,多值情况下中间用逗号分割 VDir.Properties["Path"][0] = Path; VDir.Properties["AuthFlags"][0] = iAuth; // // NT doesn't support this property // NT格式不支持这特性 // if (!IISUnderNT) { VDir.Properties["AspEnableParentPaths"][0] = true; } // // Set the changes // 设置改变 // VDir.CommitChanges(); return(true); } catch (Exception ex) { throw ex; } }
private void CreateVirtualDir() { try { DirectoryEntry IISSchema; DirectoryEntry IISAdmin; DirectoryEntry VDir; bool IISUnderNT; // // 确定IIS版本 // IISSchema = new DirectoryEntry("IIS://" + iis + "/Schema/AppIsolated"); if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN") { IISUnderNT = true; } else { IISUnderNT = false; } IISSchema.Dispose(); // // Get the admin object // 获得管理权限 // IISAdmin = new DirectoryEntry("IIS://" + iis + "/W3SVC/" + 1 + "/Root"); // // If we're not creating a root directory // 如果我们不能创建一个根目录 // // // If the virtual directory already exists then delete it // 如果虚拟目录已经存在则删除 // foreach (DirectoryEntry v in IISAdmin.Children) { if (v.Name == virtualdir) { // Delete the specified virtual directory if it already exists try { IISAdmin.Invoke("Delete", new string[] { v.SchemaClassName, virtualdir }); IISAdmin.CommitChanges(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } // // Create the virtual directory // 创建一个虚拟目录 // VDir = IISAdmin.Children.Add(virtualdir, "IIsWebVirtualDir"); // // Make it a web application // 创建一个web应用 // if (IISUnderNT) { VDir.Invoke("AppCreate", false); } else { VDir.Invoke("AppCreate", true); } // // Setup the VDir // 安装虚拟目录 //AppFriendlyName,propertyName,, bool chkRead,bool chkWrite, bool chkExecute, bool chkScript,, true, false, false, true VDir.Properties["AppFriendlyName"][0] = virtualdir; //应用程序名称 VDir.Properties["AccessRead"][0] = true; //设置读取权限 VDir.Properties["AccessExecute"][0] = false; VDir.Properties["AccessWrite"][0] = false; VDir.Properties["AccessScript"][0] = true; //执行权限[純腳本] //VDir.Properties["AuthNTLM"][0] = chkAuth; VDir.Properties["EnableDefaultDoc"][0] = true; VDir.Properties["EnableDirBrowsing"][0] = false; VDir.Properties["DefaultDoc"][0] = "Login.aspx"; //设置默认文档,多值情况下中间用逗号分割 VDir.Properties["Path"][0] = physicaldir; VDir.Properties["AuthFlags"][0] = 0x00000004 | 0x00000001; // // NT doesn't support this property // NT格式不支持这特性 // if (!IISUnderNT) { VDir.Properties["AspEnableParentPaths"][0] = true; } // // Set the changes // 设置改变 // VDir.CommitChanges(); //启动aspnet_iis.exe程序 DirectoryEntry AD = VDir; string aspnetRegIIS20 = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), @"Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe"); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = new System.Diagnostics.ProcessStartInfo(); p.StartInfo.FileName = aspnetRegIIS20; string arg = AD.Path.ToUpper(); arg = arg.Substring(arg.IndexOf("W3SVC")); p.StartInfo.Arguments = "-s " + arg; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); p.WaitForExit(); string errors = p.StandardError.ReadToEnd(); if (errors != string.Empty) { MessageBox.Show(errors); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// 创建IIS虚拟目录 /// </summary> /// <param name="webSite">The web site.</param> /// <param name="vdir">The vdir.</param> /// <param name="physicalPath">The physical path.</param> /// <param name="rootDir">if set to <c>true</c> [root dir].</param> /// <param name="chkRead">if set to <c>true</c> [CHK read].</param> /// <param name="chkWrite">if set to <c>true</c> [CHK write].</param> /// <param name="chkExecute">if set to <c>true</c> [CHK execute].</param> /// <param name="chkScript">if set to <c>true</c> [CHK script].</param> /// <param name="chkAuth">if set to <c>true</c> [CHK auth].</param> /// <param name="webSiteNum">The web site num.</param> /// <param name="serverName">Name of the server.</param> /// <returns></returns> public static string CreateVDir(string webSite, string vdir, string physicalPath, bool rootDir, bool chkRead, bool chkWrite, bool chkExecute, bool chkScript, bool chkAuth, int webSiteNum, string serverName) { string sRet = String.Empty; DirectoryEntry IISSchema; DirectoryEntry IISAdmin; DirectoryEntry VDir; bool IISUnderNT; /// 确定IIS版本 IISSchema = new DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated"); if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN") { IISUnderNT = true; } else { IISUnderNT = false; } IISSchema.Dispose(); IISAdmin = new DirectoryEntry("IIS://" + serverName + "/W3SVC/" + webSiteNum + "/Root"); if (!rootDir) { // 如果虚拟目录已经存在则删除 foreach (DirectoryEntry de in IISAdmin.Children) { if (de.Name == vdir && de.SchemaClassName == SCHEMACLASS_NAME) { // Delete the specified virtual directory if it already exists try { IISAdmin.Invoke("Delete", new object[] { de.SchemaClassName, vdir }); IISAdmin.CommitChanges(); } catch (Exception ex) { sRet += ex.Message; } } } } /// 创建一个虚拟目录 try { if (!Directory.Exists(physicalPath)) { Directory.CreateDirectory(physicalPath); } if (!rootDir) { VDir = IISAdmin.Children.Add(vdir, SCHEMACLASS_NAME); } else { VDir = IISAdmin; } // 设置属性 VDir.Properties["AccessRead"][0] = chkRead; VDir.Properties["AccessExecute"][0] = chkExecute; VDir.Properties["AccessWrite"][0] = chkWrite; VDir.Properties["AccessScript"][0] = chkScript; VDir.Properties["AuthNTLM"][0] = chkAuth; VDir.Properties["EnableDefaultDoc"][0] = true; VDir.Properties["EnableDirBrowsing"][0] = false; VDir.Properties["DefaultDoc"][0] = "default.aspx,default.html,index.aspx,index.html"; VDir.Properties["Path"][0] = physicalPath; if (!IISUnderNT) { VDir.Properties["AspEnableParentPaths"][0] = true; } VDir.CommitChanges(); // 提交更改 if (IISUnderNT) { VDir.Invoke("AppCreate", false); } else { VDir.Invoke("AppCreate", 1); } } catch (Exception ex) { throw ex; } sRet += "VRoot " + vdir + " created!"; return(sRet); }