C# 从iis获取ftp会话信息

C# 从iis获取ftp会话信息,c#,iis-7.5,C#,Iis 7.5,我正在尝试使用C#、Windows 2008和IIS 7.5检索IIS FTP的会话信息。我需要登录用户等信息。尝试以下操作: using System; using Microsoft.Web.Administration; internal static class Sample { private static void Main() { using (ServerManager serverManager = new ServerManager())

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

尝试以下操作:

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;
    }
}
使用系统;
使用Microsoft.Web.Administration;
内部静态类示例
{
私有静态void Main()
{
使用(ServerManager=newservermanager())
{
Configuration config=serverManager.GetApplicationHostConfiguration();
ConfigurationSection sitesSection=config.GetSection(“system.applicationHost/sites”);
ConfigurationElementCollection sitesCollection=sitesSection.GetCollection();
ConfigurationElement siteElement=FindElement(sitesCollection,“site”,“name”,“YOURSITENAMEHERE”);
如果(siteElement==null)抛出新的InvalidOperationException(“未找到元素!”);
ConfigurationElement ftpServerElement=siteElement.GetChildElement(“ftpServer”);
ConfigurationElementCollection SessionElement=ftpServerElement.GetChildElement(“会话”).GetCollection();
WriteLine(String.Format(“活动会话:{0}”,sessionsElement.Count));
foreach(sessionElement中的ConfigurationElement sessionElement)
{
Console.WriteLine(String.Format(“\t会话ID:{0}”),
sessionElement.Attributes[“sessionId”].Value.ToString());
Console.WriteLine(String.Format(“\t\t用户名称:{0}”),
sessionElement.Attributes[“userName”].Value.ToString());
Console.WriteLine(String.Format(“\t\t前一个命令:{0}”),
属性[“previousCommand”].Value.ToString());
}
}
}
私有静态ConfigurationElement FindElement(ConfigurationElementCollection集合,string elementTagName,params string[]keyValues)
{
foreach(集合中的ConfigurationElement)
{
if(String.Equals(element.ElementTagName、ElementTagName、StringComparison.OrdinalIgnoreCase))
{
布尔匹配=真;
对于(int i=0;i
嗯,梅菲兹

参考: