Iis 7 使用WMI使用C创建IIS应用程序目录#

Iis 7 使用WMI使用C创建IIS应用程序目录#,iis-7,iis-6,wmi,Iis 7,Iis 6,Wmi,我们在Windows 2003和Windows 2008系统上安装了一个web应用程序。过去,我们的安装代码使用ADSI在IIS中创建两个应用程序目录,但这需要在Windows 2008中安装IIS 6管理组件。我一直在尝试使用WMI创建应用程序目录,这样我们就可以支持这两种操作系统 我一直在尝试这个代码 public static void AddVirtualFolder(string serverName, string websiteId, string name, string

我们在Windows 2003和Windows 2008系统上安装了一个web应用程序。过去,我们的安装代码使用ADSI在IIS中创建两个应用程序目录,但这需要在Windows 2008中安装IIS 6管理组件。我一直在尝试使用WMI创建应用程序目录,这样我们就可以支持这两种操作系统

我一直在尝试这个代码

   public static void AddVirtualFolder(string serverName, string websiteId, string name, string path)
    {
        ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", serverName));
        scope.Connect();

        string siteName = string.Format("W3SVC/{0}/Root/{1}", websiteId, name);

        ManagementClass mc = new ManagementClass(scope, new ManagementPath("IIsWebVirtualDirSetting"), null);
        ManagementObject oWebVirtDir = mc.CreateInstance();

        oWebVirtDir.Properties["Name"].Value = siteName;
        oWebVirtDir.Properties["Path"].Value = path;
        oWebVirtDir.Properties["AuthFlags"].Value = 5; // Integrated Windows Auth.
        oWebVirtDir.Properties["EnableDefaultDoc"].Value = true;
        // date, time, size, extension, longdate ;
        oWebVirtDir.Properties["DirBrowseFlags"].Value = 0x4000003E;
        oWebVirtDir.Properties["AccessFlags"].Value = 513; // read script 
        oWebVirtDir.Put();

        ManagementObject mo = new ManagementObject(scope, new System.Management.ManagementPath("IIsWebVirtualDir='" + siteName + "'"), null);
        ManagementBaseObject inputParameters = mo.GetMethodParameters("AppCreate2");
        inputParameters["AppMode"] = 2;
        mo.InvokeMethod("AppCreate2", inputParameters, null);
        mo = new ManagementObject(scope, new System.Management.ManagementPath("IIsWebVirtualDirSetting='" + siteName + "'"), null);
        mo.Properties["AppFriendlyName"].Value = name;
        mo.Put();
    }
}

但是,我在已知目录上发现路径未找到错误。如果有人有我可以使用的参考资料,我将不胜感激。关于如何实现这一点的任何其他建议也都是受欢迎的。

使用上述代码,您仍然需要Windows 2008/IIS7上的IIS6兼容性位。原因是,设置属性(如
DirBrowseFlags
AccessFlags
等)的调用是没有IIS6管理组件的IIS7不支持的IIS 6元数据库属性

对于IIS7,我建议直接针对
Microsoft.Web.Administration
命名空间进行编程,但如果确实需要使用WMI,请参阅本文:


我曾希望能够为IIS6或IIS7使用单一代码路径,因此我避免使用IIS7特定的Microsoft.Web.Administration类。然而,我想我只需要检测IIS版本,继续使用ADSI来处理IIS6,并在IIS7的另一个代码路径中使用新的内容。谢谢,@Shane-另一个很好的原因是IIS6 compat shim还使用
AboCustomMapper
对象来预测
HandlerMappings
(类似于IIS6 scriptmaps),这些对象不能利用启动先决条件等功能。相信我,保持良好的
applicationHost.config
文件卫生是值得的。我不太理解您使用Web.Administration的建议,因为Microsoft.Web.Administration只能欣赏IIS7+服务器。我们是假设IIS7吗?