【问题标题】:Use WMI to create IIS Application Directory with C#使用 WMI 使用 C# 创建 IIS 应用程序目录
【发布时间】:2010-01-22 18:25:56
【问题描述】:

我们有一个安装在 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();
    }
}

但是,我在已知目录中发现路径未找到错误。如果有人有一些我可以使用的参考资料,我将不胜感激。也欢迎任何其他关于如何解决此问题的建议。

【问题讨论】:

    标签: iis-7 iis-6 wmi


    【解决方案1】:

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

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

    Managing Sites with IIS 7.0's WMI Provider (IIS.NET)

    【讨论】:

    • 我曾希望能够为 IIS6 或 IIS7 使用单一代码路径,因此我避免使用 IIS7 特定的 Microsoft.Web.Administration 类。但是,我想我只需要检测 IIS 版本并继续使用 IIS6 的 ADSI 并在 IIS7 的另一个代码路径中使用新的东西。谢谢,
    • @Shane - 另一个很好的理由是 IIS6 兼容 shim 也将 HandlerMappings(类似于 IIS6 脚本映射)与 AboCustomMapper 对象结合起来,这些对象不能利用诸如启动前的功能。条件。相信我,保持良好的applicationHost.config 文件卫生是值得的。
    • 我不太明白您对使用 Web.Administration 的建议,因为 Microsoft.Web.Administation 只能管理到/从 IIS7+ 服务器。我们假设是 IIS7 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多