zoukankan      html  css  js  c++  java
  • C#获取windows所有用户名

    #region 验证操作系统用户名
            public bool isExistUserName(string name)
            {
                List<string> nameList = GetSysUserNames();
                return nameList.Contains(name);
            }

            public List<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;
            }
            [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);
            #endregion

  • 相关阅读:
    Apache Beam的特点
    Apache Beam是什么?
    Kudu1.1.0 、 Kudu1.2.0 Kudu1.3.0的版本信息异同比较
    Kudu compaction design
    [转]Oracle trunc()函数的用法
    [转]Charts (Report Builder and SSRS)
    [转]表变量和临时表的比较
    [转]MONTHS_BETWEEN Function
    [转]Grunt 新手一日入门
    [转]Format a ui-grid grid column as currency
  • 原文地址:https://www.cnblogs.com/futao/p/2042138.html
Copyright © 2011-2022 走看看