我想感谢Bryan Roach,因为他的帮助我解决了我的问题。我也意识到我可以将我的C#项目构建设置为平台目标x64,从而避免错误,因为它会搜索64位注册表区域。然而,我认为对于我的应用程序来说,选择Any CPU更为合适,让程序本身能够解决这个问题。
string ServerName = "REMOTE_COMPUTER";
PrincipalSearcher pSearch = new PrincipalSearcher();
pSearch.QueryFilter = new UserPrincipal(new PrincipalContext(ContextType.Machine, ServerName, null, ContextOptions.Negotiate));
try
{
foreach (UserPrincipal userUP in pSearch.FindAll())
{
//Missing Registry Keys will error on pSearch.FindAll();
//Either Build > Platform Target == x64 or deal with it.
}
}
catch(FileNotFoundException ex)
{
if(ex.Source.Equals("Active Directory") &&
ex.TargetSite.MemberType.ToString().Equals("Method") &&
ex.TargetSite.Name.Equals("GetInfo"))
{
//It's possible the registry keys haven't been moved to x86 location on a 64 bit machine:
//From: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion (64 bit)
//To: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion (32 bit compatability area)
//String Properties need to be present: RegisteredOwner, RegisteredOrganization
try
{
Hack_Fixx64RegistryForGettingLocalAccounts(ServerName);
//Recall function or whatever to try again with fixed registry.
}
catch
{ }
}
}
然后是将注册表键复制到正确位置的函数:
private void Hack_Fixx64RegistryForGettingLocalAccounts(string ServerName)
{
RegistryKey remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName, RegistryView.Registry64);
if(remoteKey != null)
{
//Get keys stored on 64 bit location
RegistryKey x64regkey = remoteKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
string regOwner = Convert.ToString(x64regkey.GetValue("RegisteredOwner", ""));
string regOrganization = Convert.ToString(x64regkey.GetValue("RegisteredOrganization", ""));
//Add missing keys on 64 bit OS in correct location for 32 bit registry area. The Wow6432Node is for 32-bit apps that run on 64-bit window versions.
remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName, RegistryView.Registry32);
if(remoteKey != null)
{
RegistryKey x86regkey = remoteKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", true);
x86regkey.SetValue("RegisteredOwner", regOwner);
x86regkey.SetValue("RegisteredOrganization", regOrganization);
}
}
}
WinNT://
更改为适用于Windows 7机器。这只是一个猜测。 - MalachiD:\GetDirectoryEnttryProperties\Program.cs:line 15
,而.cs
是C#
文件的扩展名。 - Malachi