程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#獲取本機所有用戶組和用戶名

C#獲取本機所有用戶組和用戶名

編輯: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
            //

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved