首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从iis获取ftp会话信息

问从iis获取ftp会话信息
EN

Stack Overflow用户
提问于 2010-09-27 18:34:31
回答 1查看 679关注 0票数 0

我正在尝试使用C#、Windows2008和IIS7.5检索IIS的会话信息。我需要像登录用户等信息。

EN

回答 1

Stack Overflow用户

发布于 2011-02-08 23:42:56

试试这个:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3802991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档