从iis获取ftp会话信息

时间:2010-09-27 10:34:31

标签: c# iis-7.5

我正在尝试使用C#,Windows 2008和IIS 7.5检索IIS FTP的会话信息。我需要登录用户等信息。

1 个答案:

答案 0 :(得分:0)

试试这个:

using System;
using Microsoft.Web.Administration;

internal static class Sample
{
    private static void Main()
    {
        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();
            ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
            ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

            ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", "YOURSITENAMEHERE");
            if (siteElement == null) throw new InvalidOperationException("Element not found!");

            ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer");
            ConfigurationElementCollection sessionsElement = ftpServerElement.GetChildElement("sessions").GetCollection();

            Console.WriteLine(String.Format("Active Sessions: {0}", sessionsElement.Count));

            foreach (ConfigurationElement sessionElement in sessionsElement)
            {


                Console.WriteLine(String.Format("\tSession ID: {0}",
                    sessionElement.Attributes["sessionId"].Value.ToString()));
                Console.WriteLine(String.Format("\t\tUser Name: {0}",
                    sessionElement.Attributes["userName"].Value.ToString()));
                Console.WriteLine(String.Format("\t\tPrevious Command: {0}",
                    sessionElement.Attributes["previousCommand"].Value.ToString()));
            }
        }
    }

    private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
    {
        foreach (ConfigurationElement element in collection)
        {
            if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
            {
                bool matches = true;
                for (int i = 0; i < keyValues.Length; i += 2)
                {
                    object o = element.GetAttributeValue(keyValues[i]);
                    string value = null;
                    if (o != null)
                    {
                        value = o.ToString();
                    }
                    if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
                    {
                        matches = false;
                        break;
                    }
                }
                if (matches)
                {
                    return element;
                }
            }
        }
        return null;
    }
}

HTH,MemphiZ

参考:http://www.iis.net/configreference/system.applicationhost/sites/site/ftpserver/sessions