C# 获得系统所有用户和当前登录用户(当前进程的)

 1 using System;
2 using System.Collections.Generic;
3 using System.Runtime.InteropServices;
4 using System.Security.Principal;
5 using System.Text;
6
7 namespace SystemUsers
8 {
9 class Program
10 {
11 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
12 public struct USER_INFO_0
13 {
14 public string UserName;
15 }
16
17 [DllImport("Netapi32.dll")]
18 extern static int NetUserEnum(
19 [MarshalAs(UnmanagedType.LPWStr)]
20 string servername,
21 int level,
22 int filter,
23 out IntPtr bufptr,
24 int prefmaxlen,
25 out int entriesread,
26 out int totalentries,
27 out int resume_handle);
28
29 [DllImport("Netapi32.dll")]
30 extern static int NetApiBufferFree(IntPtr Buffer);
31
32 [DllImport("Advapi32.dll", EntryPoint = "GetUserName", ExactSpelling = false, SetLastError = true)]
33 static extern bool GetUserName(
34 [MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
35 [MarshalAs(UnmanagedType.LPArray)] Int32[] nSize);
36
37 public static List<string> GetAllUsersOfSystem()
38 {
39 List<string> users = new List<string>();
40
41 int EntriesRead;
42 int TotalEntries;
43 int Resume;
44 IntPtr bufPtr;
45
46 NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
47
48 if (EntriesRead > 0)
49 {
50 USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
51 IntPtr iter = bufPtr;
52 for (int i = 0; i < EntriesRead; i++)
53 {
54 Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
55 iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
56 users.Add(Users[i].UserName);
57 }
58 NetApiBufferFree(bufPtr);
59 }
60
61 return users;
62 }
63
64 public static string GetCurrentUserOfSystemByAPI()
65 {
66 byte[] bytes = new byte[256];
67 Int32[] len = new Int32[1];
68 len[0] = 256;
69 GetUserName(bytes, len);
70
71 return Encoding.ASCII.GetString(bytes).Trim('\0');
72 }
73
74 public static string GetCurrentUserOfSystem()
75 {
76 return WindowsIdentity.GetCurrent().Name;
77 }
78
79 static void Main(string[] args)
80 {
81 List<string> users = GetAllUsersOfSystem();
82
83 Console.WriteLine("All users of System:");
84 foreach (string user in users)
85 {
86 Console.WriteLine(user);
87 }
88 //Administrator
89 //Guest
90 //Peter
91 //v-petz
92
93 Console.WriteLine("The logged user of the System(by API):");
94 Console.WriteLine(GetCurrentUserOfSystemByAPI()); //Peter
95 Console.WriteLine("The logged user of the System(by .net method):");
96 Console.WriteLine(GetCurrentUserOfSystem()); //Peter-PC\Peter
97 }
98 }
99 }

调用API 需要使用 System.Runtime.InteropServices 命名空间。

NetUserEnum

The NetUserEnum function retrieves information about all user accounts on a server.

参考:http://msdn.microsoft.com/en-us/library/aa370652(v=vs.85).aspx

NetApiBufferFree

The NetApiBufferFree function frees the memory that the NetApiBufferAllocate function allocates. Applications should also call NetApiBufferFree to free the memory that other network management functions use internally to return information.

参考:http://msdn.microsoft.com/en-us/library/aa370304(VS.85).aspx

GetUserName
Retrieves the name of the user associated with the current thread.

参考:http://msdn.microsoft.com/en-us/library/ms724432(v=VS.85).aspx

posted on 2011-07-25 21:59  PeterZhang  阅读(5122)  评论(0)    收藏  举报