我需要一种方法来获取(Windows)计算机上所有用户帐户的列表,我需要通过C#.NET 1.1 + 2003获取此信息
答案 0 :(得分:7)
您可以使用WMI枚举本地用户和组:
class Program
{
static void Main(string[] args)
{
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='mypcname'");
try
{
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
Console.WriteLine("User Accounts");
Console.WriteLine();
foreach (ManagementObject mObject in mSearcher.Get())
{
Console.WriteLine("Account {0}", mObject["Name"]);
foreach (PropertyData prop in mObject.Properties)
{
Console.WriteLine("Name: {0}\tValue: {1}", prop.Name, prop.Value);
}
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
上面的代码需要引用System.Management
,您需要将'mypcname'替换为您的机器名称。
答案 1 :(得分:4)
您需要使用WMI来获取所需的信息。
添加对System.Management的引用,下面的代码将完全符合您的需要:)
using System;
using System.Management;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_UserAccount Where LocalAccount = True");
ManagementObjectCollection results = mos.Get();
foreach (ManagementObject user in results)
{
Console.WriteLine("Account Type: " + user["AccountType"].ToString());
Console.WriteLine("Caption: " + user["Caption"].ToString());
Console.WriteLine("Description: " + user["Description"].ToString());
Console.WriteLine("Disabled: " + user["Disabled"].ToString());
Console.WriteLine("Domain: " + user["Domain"].ToString());
Console.WriteLine("Full Name: " + user["FullName"].ToString());
Console.WriteLine("Local Account: " + user["LocalAccount"].ToString());
Console.WriteLine("Lockout: " + user["Lockout"].ToString());
Console.WriteLine("Name: " + user["Name"].ToString());
Console.WriteLine("Password Changeable: " + user["PasswordChangeable"].ToString());
Console.WriteLine("Password Expires: " + user["PasswordExpires"].ToString());
Console.WriteLine("Password Required: " + user["PasswordRequired"].ToString());
Console.WriteLine("SID: " + user["SID"].ToString());
Console.WriteLine("SID Type: " + user["SIDType"].ToString());
Console.WriteLine("Status: " + user["Status"].ToString());
}
Console.ReadKey();
}
}
}