我试图测试我为 c# 构建的新 dll
private void button1_Click(object sender, EventArgs e)
{
String [] first = UserQuery.Get_All_Users();
//MessageBox.Show(first);
}
但我收到以下错误String [] first = UserQuery.Get_All_Users();
User_Query.dll 中出现“System.NullReferenceException”类型的未处理异常
附加信息:对象引用未设置为对象的实例。
我一直想弄清楚这个几个小时,但找不到任何空变量
我发布我的 dll 以防 dll 错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace User_Query
{
public class UserQuery
{
public static string[] Get_All_Users()
{
string[] names = new string[10];
var path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (var computerEntry = new DirectoryEntry(path))
{
var userNames = from DirectoryEntry childEntry in computerEntry.Children
where childEntry.SchemaClassName == "User"
select childEntry.Name;
byte i = 0;
foreach (var name in userNames)
{
Console.WriteLine(name);
names[i] = name;
i++;
}
return names;
}
}
}
}