枚举网络会话

发布于 2024-08-24 09:52:52 字数 144 浏览 6 评论 0原文

我想从计算机管理 -> 中提取有关已连接网络用户的数据共享文件夹->会话选项卡进入我的 C# 应用程序。任何人都可以指导我必须使用哪些命名空间以及一些示例代码来从计算机管理导入用户名和 IP 地址 ->共享文件夹->会话选项卡?

问候

I wanted to pull data about the connected network users from the Computer Management -> Shared Folders -> Sessions tab into my c# application. Can anybody guide me on what namespaces have to be used along with some sample code to import username and ip address from Computer Management -> Shared Folders -> Sessions tab?

Regards

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

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

发布评论

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

评论(2

枕头说它不想醒 2024-08-31 09:52:53

您需要 NetSessionEnum,其中:

提供有关在服务器上建立的会话的信息。

当通过 502 级别时,它将返回一个数组:

...计算机的名称;用户姓名;打开计算机上的文件、管道和设备;以及客户端正在使用的传输的名称。

幸运的是,pinvoke.net 拥有必要的签名,甚至还有一些示例代码。这是一个功能齐全的示例:

public class Program {
    public void Main(string[] args) {
        IntPtr pSessionInfo;
        IntPtr pResumeHandle = IntPtr.Zero;
        UInt32 entriesRead, totalEntries;

        var netStatus = NativeMethods.NetSessionEnum(
            null, // local computer
            null, // client name
            null, // username
            502, // include all info
            out pSessionInfo, // pointer to SESSION_INFO_502[]
            NativeMethods.MAX_PREFERRED_LENGTH,
            out entriesRead,
            out totalEntries,
            ref pResumeHandle
        );

        try {
            if (netStatus != NativeMethods.NET_API_STATUS.NERR_Success) {
                throw new InvalidOperationException(netStatus.ToString());
            }
            Console.WriteLine("Read {0} of {1} entries", entriesRead, totalEntries);
            for (int i = 0; i < entriesRead; i++) {
                var pCurrentSessionInfo = new IntPtr(pSessionInfo.ToInt32() + (NativeMethods.SESSION_INFO_502.SIZE_OF * i));
                var s = (NativeMethods.SESSION_INFO_502)Marshal.PtrToStructure(pCurrentSessionInfo, typeof(NativeMethods.SESSION_INFO_502));
                Console.WriteLine(
                    "User: {0}, Computer: {1}, Type: {2}, # Open Files: {3}, Connected Time: {4}s, Idle Time: {5}s, Guest: {6}",
                    s.sesi502_username,
                    s.sesi502_cname,
                    s.sesi502_cltype_name,
                    s.sesi502_num_opens,
                    s.sesi502_time,
                    s.sesi502_idle_time,
                    s.sesi502_user_flags == NativeMethods.SESSION_INFO_502_USER_FLAGS.SESS_GUEST
                );
            }
        } finally {
            NativeMethods.NetApiBufferFree(pSessionInfo);
        }
    }
}

public sealed class NativeMethods {
    [DllImport("netapi32.dll", SetLastError=true)]
    public static extern NET_API_STATUS NetSessionEnum(
            string serverName,
            string uncClientName,
            string userName,
            UInt32 level,
            out IntPtr bufPtr,
            int prefMaxLen,
            out UInt32 entriesRead,
            out UInt32 totalEntries,
            ref IntPtr resume_handle
    );

    [DllImport("netapi32.dll")]
    public static extern uint NetApiBufferFree(IntPtr Buffer);

    public const int MAX_PREFERRED_LENGTH = -1;

    public enum NET_API_STATUS : uint {
        NERR_Success = 0,
        NERR_InvalidComputer = 2351,
        NERR_NotPrimary = 2226,
        NERR_SpeGroupOp = 2234,
        NERR_LastAdmin = 2452,
        NERR_BadPassword = 2203,
        NERR_PasswordTooShort = 2245,
        NERR_UserNotFound = 2221,
        ERROR_ACCESS_DENIED = 5,
        ERROR_NOT_ENOUGH_MEMORY = 8,
        ERROR_INVALID_PARAMETER = 87,
        ERROR_INVALID_NAME = 123,
        ERROR_INVALID_LEVEL = 124,
        ERROR_MORE_DATA = 234 ,
        ERROR_SESSION_CREDENTIAL_CONFLICT = 1219
    }

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
    public struct SESSION_INFO_502 {
        public static readonly int SIZE_OF = Marshal.SizeOf(typeof(SESSION_INFO_502));
        public string sesi502_cname;
        public string sesi502_username;
        public uint sesi502_num_opens;
        public uint sesi502_time;
        public uint sesi502_idle_time;
        public SESSION_INFO_502_USER_FLAGS sesi502_user_flags;
        public string sesi502_cltype_name;
        public string sesi502_transport;
    }

    public enum SESSION_INFO_502_USER_FLAGS : uint {
        SESS_GUEST = 1,
        SESS_NOENCRYPTION = 2
    }
}

You want NetSessionEnum, which:

Provides information about sessions established on a server.

When passed a level of 502, it'll return an array of:

...the name of the computer; name of the user; open files, pipes, and devices on the computer; and the name of the transport the client is using.

Luckily for you, pinvoke.net has the necessary signatures and even some sample code. Here's a full featured sample:

public class Program {
    public void Main(string[] args) {
        IntPtr pSessionInfo;
        IntPtr pResumeHandle = IntPtr.Zero;
        UInt32 entriesRead, totalEntries;

        var netStatus = NativeMethods.NetSessionEnum(
            null, // local computer
            null, // client name
            null, // username
            502, // include all info
            out pSessionInfo, // pointer to SESSION_INFO_502[]
            NativeMethods.MAX_PREFERRED_LENGTH,
            out entriesRead,
            out totalEntries,
            ref pResumeHandle
        );

        try {
            if (netStatus != NativeMethods.NET_API_STATUS.NERR_Success) {
                throw new InvalidOperationException(netStatus.ToString());
            }
            Console.WriteLine("Read {0} of {1} entries", entriesRead, totalEntries);
            for (int i = 0; i < entriesRead; i++) {
                var pCurrentSessionInfo = new IntPtr(pSessionInfo.ToInt32() + (NativeMethods.SESSION_INFO_502.SIZE_OF * i));
                var s = (NativeMethods.SESSION_INFO_502)Marshal.PtrToStructure(pCurrentSessionInfo, typeof(NativeMethods.SESSION_INFO_502));
                Console.WriteLine(
                    "User: {0}, Computer: {1}, Type: {2}, # Open Files: {3}, Connected Time: {4}s, Idle Time: {5}s, Guest: {6}",
                    s.sesi502_username,
                    s.sesi502_cname,
                    s.sesi502_cltype_name,
                    s.sesi502_num_opens,
                    s.sesi502_time,
                    s.sesi502_idle_time,
                    s.sesi502_user_flags == NativeMethods.SESSION_INFO_502_USER_FLAGS.SESS_GUEST
                );
            }
        } finally {
            NativeMethods.NetApiBufferFree(pSessionInfo);
        }
    }
}

public sealed class NativeMethods {
    [DllImport("netapi32.dll", SetLastError=true)]
    public static extern NET_API_STATUS NetSessionEnum(
            string serverName,
            string uncClientName,
            string userName,
            UInt32 level,
            out IntPtr bufPtr,
            int prefMaxLen,
            out UInt32 entriesRead,
            out UInt32 totalEntries,
            ref IntPtr resume_handle
    );

    [DllImport("netapi32.dll")]
    public static extern uint NetApiBufferFree(IntPtr Buffer);

    public const int MAX_PREFERRED_LENGTH = -1;

    public enum NET_API_STATUS : uint {
        NERR_Success = 0,
        NERR_InvalidComputer = 2351,
        NERR_NotPrimary = 2226,
        NERR_SpeGroupOp = 2234,
        NERR_LastAdmin = 2452,
        NERR_BadPassword = 2203,
        NERR_PasswordTooShort = 2245,
        NERR_UserNotFound = 2221,
        ERROR_ACCESS_DENIED = 5,
        ERROR_NOT_ENOUGH_MEMORY = 8,
        ERROR_INVALID_PARAMETER = 87,
        ERROR_INVALID_NAME = 123,
        ERROR_INVALID_LEVEL = 124,
        ERROR_MORE_DATA = 234 ,
        ERROR_SESSION_CREDENTIAL_CONFLICT = 1219
    }

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
    public struct SESSION_INFO_502 {
        public static readonly int SIZE_OF = Marshal.SizeOf(typeof(SESSION_INFO_502));
        public string sesi502_cname;
        public string sesi502_username;
        public uint sesi502_num_opens;
        public uint sesi502_time;
        public uint sesi502_idle_time;
        public SESSION_INFO_502_USER_FLAGS sesi502_user_flags;
        public string sesi502_cltype_name;
        public string sesi502_transport;
    }

    public enum SESSION_INFO_502_USER_FLAGS : uint {
        SESS_GUEST = 1,
        SESS_NOENCRYPTION = 2
    }
}
眼泪淡了忧伤 2024-08-31 09:52:53

您不想与计算机管理交互,您想调用它所执行的相同功能并将中间人排除在循环之外。

要了解当前的网络连接,您可能需要查找 WNetOpenEnumNetConnectionEnum

我不知道认为 BCL 中没有 .NET 函数,您必须使用 p/invoke 或寻找第三方库(或者我个人最喜欢的,使用 C++/CLI 编写包装器)

You don't want to interact with Computer Management, you want to call the same functions it does and cut the middle man out of the loop.

For finding out about current network connection, you might be looking for WNetOpenEnum or NetConnectionEnum

I don't think there's a .NET function for this in the BCL, you'd have to use p/invoke or look for a third-party library (or my personal favorite, use C++/CLI to write a wrapper)

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