如何使用WMI读取IIS 6网站的目录结构? [英] How to read an IIS 6 Website's Directory Structure using WMI?

查看:803
本文介绍了如何使用WMI读取IIS 6网站的目录结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读网站的文件夹在IIS 6.0中使用WMI和C#。我能够读取使用IISWebVirtualDirSetting级的虚拟目录和应用程序。不过在位于一个网站内的物理文件夹无法使用此类读取。

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.

并为我的情况下,我需要读取位于网站内的子文件,后来他们设置权限。对于我的要求,我不需要在虚拟目录/ Web服务应用程序(可以使用下面的代码很容易获得。)工作。

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..).

我曾尝试使用IISWebDirectory类,但它是有用的。

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

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

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;
}



PS:

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

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.

我不PowerShell中工作,我并不真的需要涉及的PowerShell或VB脚本的解决方案。

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

在C#中做同样的任何替代编程方式/ ASP.Net也将高度赞赏

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

编辑:

中的代码可能是这样的:

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;
}

下面是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

推荐答案

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

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.

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

如果WMI已经在那里那么Win32_Directory是应在查询中使用的类:

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 + "%'";



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

这篇关于如何使用WMI读取IIS 6网站的目录结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆