当前位置:编程学习 > C#/ASP.NET >>

C#获取本机所有用户组和用户名

\

internal class Win32API
{
#region Win32 API Interfaces
    [DllImport( "netapi32.dll", EntryPoint = "NetApiBufferFree" )]
    internal static extern void NetApiBufferFree(IntPtr bufptr);

    [DllImport( "netapi32.dll", EntryPoint = "NetLocalGroupGetMembers" )]
    internal static extern uint NetLocalGroupGetMembers(
        IntPtr ServerName,
        IntPtr GrouprName,
        uint level,
        ref IntPtr siPtr,
        uint prefmaxlen,
        ref uint entriesread,
        ref uint totalentries,
        IntPtr resumeHandle);

    [DllImport( "netapi32.dll", EntryPoint = "NetLocalGroupEnum" )]
    internal static extern uint NetLocalGroupEnum(
        IntPtr ServerName,
        uint level,
        ref IntPtr siPtr,
        uint prefmaxlen,
        ref uint entriesread,
        ref uint totalentries,
        IntPtr resumeHandle);

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    internal struct LOCALGROUP_MEMBERS_INFO_1
    {
        public IntPtr lgrmi1_sid;
        public IntPtr lgrmi1_sidusage;
        public IntPtr lgrmi1_name;

    }

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    internal struct LOCALGROUP_INFO_1
    {
        public IntPtr lpszGroupName;
        public IntPtr lpszComment;
    }
#endregion
}
 

private void groupbtn_Click(object sender, System.EventArgs e)
{
    //defining values for getting group names
    uint level = 1, prefmaxlen = 0xFFFFFFFF,
        entriesread = 0, totalentries = 0;

    //Values that will receive information.
    IntPtr GroupInfoPtr,UserInfoPtr;
    GroupInfoPtr = IntPtr.Zero;
    UserInfoPtr = IntPtr.Zero;

    Win32API.NetLocalGroupEnum(
        IntPtr.Zero, //Server name.it must be null
        level,//level can be 0 or 1 for groups.
            //For more information see LOCALGROUP_INFO_0
            //and LOCALGROUP_INFO_1
        ref GroupInfoPtr,//Value that will be receive information
        prefmaxlen,//maximum length
        ref entriesread,//value that receives the count of
            //elements actually enumerated.
        ref totalentries,//value that receives the approximate total
            //number of entries that could have been
            //

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,