• C#获取本机所有用户名


    using System.DirectoryServices;

    using System.Runtime.InteropServices;

    (需要添加引用)

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct USER_INFO_0
            {
                public string Username;
            }
            [DllImport("Netapi32.dll ")]
            extern static int NetUserEnum(
                    [MarshalAs(UnmanagedType.LPWStr)]   
            string servername,
                    int level,
                    int filter,
                    out   IntPtr bufptr,
                    int prefmaxlen,
                    out   int entriesread,
                    out   int totalentries,
                    out   int resume_handle);
    
            [DllImport("Netapi32.dll ")]
            extern static int NetApiBufferFree(IntPtr Buffer);
    
            public string[] GetSysUserNames()
            {
                int EntriesRead;
                int TotalEntries;
                int Resume;
                IntPtr bufPtr;
                List<string> temp = new List<string>();
    
                NetUserEnum(null, 0, 2, out   bufPtr, -1, out   EntriesRead,
                        out   TotalEntries, out   Resume);
                if (EntriesRead > 0)
                {
                    USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
                    IntPtr iter = bufPtr;
                    for (int i = 0; i < EntriesRead; i++)
                    {
                        Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter,
                                typeof(USER_INFO_0));
                        iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
                        temp.Add(Users[i].Username);
                    }
                    NetApiBufferFree(bufPtr);
                }
                return temp.ToArray();
            }
  • 相关阅读:
    LeetCode 8 有效的括号
    String源码学习
    LeetCode 7最长公共前缀
    LeetCode 5回文数
    LeetCode 6罗马数字转整数
    LeetCode 4.反转整数
    LeetCode 3.无重复字符的最长子串
    区分子串和子序列
    LeetCode 1.两数之和
    一个十分好用的动画工具:Velocity.js
  • 原文地址:https://www.cnblogs.com/xxxteam/p/3316534.html
Copyright © 2020-2023  润新知