如何使用 WMI 读取 IIS 6 网站的目录结构?

发布于 2024-08-26 19:51:44 字数 3730 浏览 0 评论 0原文

我需要在 IIS 6.0 中使用 WMI 和 C# 读取网站的文件夹。我可以使用“IISWebVirtualDirSetting”类读取虚拟目录和应用程序。但是无法使用此类读取位于网站内的物理文件夹。

对于我的情况,我需要读取位于网站内的子文件夹然后对它们设置权限。根据我的要求,我不需要处理虚拟目录/Web 服务应用程序(可以使用下面的代码轻松获得......)。

我尝试过使用 IISWebDirectory 类,但它很有用。

下面是读取 IIS 虚拟目录的代码...

public static ArrayList RetrieveVirtualDirList(String ServerName, String WebsiteName)
{
    ConnectionOptions options = SetUpAuthorization();
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
    scope.Connect();
    String SiteId = GetSiteIDFromSiteName(ServerName, WebsiteName);
    ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IISWebVirtualDirSetting");
    //ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");

    ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
    ArrayList WebSiteListArray = new ArrayList();
    ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
    String WebSiteName = String.Empty;
    foreach (ManagementObject WebSite in WebSitesCollection)
    {
        WebSiteName = WebSite.Properties["Name"].Value.ToString();
        WebsiteName = WebSiteName.Replace("W3SVC/", "");
        String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
        String temp = WebsiteName.Substring(0, WebsiteName.IndexOf('/') + 1);
        String VirtualDirName = WebsiteName.Substring(temp.Length);
        WebsiteName = WebsiteName.Replace(SiteId, "");
        if (extrctedSiteId.Equals(SiteId))
        //if (true)
        {
            WebSiteListArray.Add(VirtualDirName );
            //WebSiteListArray.Add(WebSiteName);
            //+ "|" + WebSite.Properties["Path"].Value.ToString()
        }
    }
    return WebSiteListArray;
}

PS:

我需要在 ASP 中使用 WMI 和 C# 以编程方式获取已部署站点的子文件夹。网络应用程序。我需要找出本地或远程 IIS 6.0 Web 服务器中现有网站的子文件夹。所以我需要一个程序化的解决方案。确切地说,如果我指向正确的类(如 IISWebVirtualDirSetting 等),我可以使用它来检索网站内的物理文件夹列表,那么它将非常有帮助。

我不在 Powershell 中工作,并且我并不真正需要涉及 powershell 或 vbscripts 的解决方案。

在 C#/ASP.Net 中执行相同操作的任何替代编程方式也将受到高度赞赏。

编辑:

GetSiteIdFromSiteName

代码可能如下所示:

public static int GetSiteIdFromSiteName(String ServerName, String WebsiteName)
{
    ConnectionOptions options = SetUpAuthorization();
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
    scope.Connect();
    ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");

    ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
    ArrayList WebSiteListArray = new ArrayList();
    ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
    String WebSiteName = String.Empty;
    foreach (ManagementObject WebSite in WebSitesCollection)
    {
        WebSiteName = WebSite.Properties["Name"].Value.ToString();
        WebsiteName = WebSiteName.Replace("W3SVC/", "");
        String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
        break;  
    }
    return extrctedSiteId;
}

SetupAuthorization()

这是SetupAuthorization 函数。

    public ConnectionOptions SetUpAuthorization()
{ 
        ConnectionOptions options = new ConnectionOptions();
    //options.Authentication = AuthenticationLevel.Connect; 
    //may need to set Packetprivacy enum
    options.Authentication = AuthenticationLevel.PacketPrivacy; 
    options.EnablePrivileges = true; 
    options.Impersonation = ImpersonationLevel.Impersonate; 

     return options;
 } 

问候

I need to read a website's folders using WMI and C# in IIS 6.0. I am able to read the Virtual directories and applications using the "IISWebVirtualDirSetting" class. However the physical folders located inside a website cannot be read using this class.

And for my case i need to read sub folders located within a website and later on set permission on them. For my requirement i dont need to work on Virtual Directories/Web Service Applications (which can be easily obtained using the code below..).

I have tried to use IISWebDirectory class but it has been useful.

Here is the code that reads IIS Virtual Directories...

public static ArrayList RetrieveVirtualDirList(String ServerName, String WebsiteName)
{
    ConnectionOptions options = SetUpAuthorization();
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
    scope.Connect();
    String SiteId = GetSiteIDFromSiteName(ServerName, WebsiteName);
    ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IISWebVirtualDirSetting");
    //ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");

    ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
    ArrayList WebSiteListArray = new ArrayList();
    ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
    String WebSiteName = String.Empty;
    foreach (ManagementObject WebSite in WebSitesCollection)
    {
        WebSiteName = WebSite.Properties["Name"].Value.ToString();
        WebsiteName = WebSiteName.Replace("W3SVC/", "");
        String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
        String temp = WebsiteName.Substring(0, WebsiteName.IndexOf('/') + 1);
        String VirtualDirName = WebsiteName.Substring(temp.Length);
        WebsiteName = WebsiteName.Replace(SiteId, "");
        if (extrctedSiteId.Equals(SiteId))
        //if (true)
        {
            WebSiteListArray.Add(VirtualDirName );
            //WebSiteListArray.Add(WebSiteName);
            //+ "|" + WebSite.Properties["Path"].Value.ToString()
        }
    }
    return WebSiteListArray;
}

P.S:

I need to programmatically get the sub folders of an already deployed site(s) using WMI and C# in an ASP. Net Application. I need to find out the sub folders of existing websites in a local or remote IIS 6.0 Web Server. So i require a programmatic solution. Precisely if i am pointed at the right class (like IISWebVirtualDirSetting etc ) that i may use for retrieving the list of physical folders within a website then it will be quite helpful.

I am not working in Powershell and i don't really need a solution that involves powershell or vbscripts.

Any alternative programmatic way of doing the same in C#/ASP.Net will also be highly appreciated.

EDIT:

GetSiteIdFromSiteName

The code may look like this:

public static int GetSiteIdFromSiteName(String ServerName, String WebsiteName)
{
    ConnectionOptions options = SetUpAuthorization();
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
    scope.Connect();
    ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");

    ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
    ArrayList WebSiteListArray = new ArrayList();
    ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
    String WebSiteName = String.Empty;
    foreach (ManagementObject WebSite in WebSitesCollection)
    {
        WebSiteName = WebSite.Properties["Name"].Value.ToString();
        WebsiteName = WebSiteName.Replace("W3SVC/", "");
        String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
        break;  
    }
    return extrctedSiteId;
}

and

SetupAuthorization()

Here is the SetupAuthorization function.

    public ConnectionOptions SetUpAuthorization()
{ 
        ConnectionOptions options = new ConnectionOptions();
    //options.Authentication = AuthenticationLevel.Connect; 
    //may need to set Packetprivacy enum
    options.Authentication = AuthenticationLevel.PacketPrivacy; 
    options.EnablePrivileges = true; 
    options.Impersonation = ImpersonationLevel.Impersonate; 

     return options;
 } 

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2024-09-02 19:51:44

没关系。我自己得到的。
如果目录结构位于本地系统上,则需要 System.IO.DirectoryInfo。使用远程处理同样可以用于远程服务器。

参考:
http://msdn.microsoft.com/en-us/ library/system.io.directoryinfo.aspx

如果 WMi 必须在那里,那么 Win32_Directory 是应该在查询中使用的类:

"Select * from Win32_directory where drive ='"+ DriveLetter +":' and Path ='%" + MyPath + "%'";

参考:
http://msdn.microsoft.com/en-us /library/aa394130(VS.85).aspx

Never mind. i got it myself.
If the directory structure is on local system the System.IO.DirectoryInfo is what is needed. Using remoting the same can be used for remote server as well.

Reference:
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

If WMi has to be there then Win32_Directory is the class which should used in query:

"Select * from Win32_directory where drive ='"+ DriveLetter +":' and Path ='%" + MyPath + "%'";

Reference:
http://msdn.microsoft.com/en-us/library/aa394130(VS.85).aspx

缘字诀 2024-09-02 19:51:44

您看过 Web 部署工具吗?

http://www.iis.net/expand/WebDeploy

能够打包 ACL、COM、 GAC 和注册表设置。

Have you looked at the Web Deployment Tool?

http://www.iis.net/expand/WebDeploy

Ability to package ACLs, COM, GAC and registry settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文