所以我有一些代码可以查询特定计算机对象的多个域,并将结果输出到csv。这是我的代码。
class Program
{
static void Main(string[] args)
{
try
{
string log = "C:\\Users\\User\\Desktop\\New.csv";
using (FileStream file = new FileStream(log, FileMode.Append, FileAccess.Write))
{
StreamWriter writer = new StreamWriter(file);
var domains = File.ReadLines("C:\\Users\\User\\Desktop\\domains.csv");
foreach (var line in domains)
{
PrincipalContext AD = new PrincipalContext(ContextType.Domain, line.ToString());
ComputerPrincipal query = new ComputerPrincipal(AD);
query.Name = "COMPNAME";
PrincipalSearcher search = new PrincipalSearcher();
search.QueryFilter = query;
ComputerPrincipal result = (ComputerPrincipal)search.FindOne();
if (result != null)
{
writer.WriteLine(result.DistinguishedName);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
}
domains.csv文件包含我们所有的10个域。会发生什么result
始终显示为null
。我错过了什么?
答案 0 :(得分:1)
代码没有问题。这是我的作家遇到的问题。我用这个替换了我的作家
using (TextWriter writer = new StreamWriter(log))