ブライアンローチのおかげで問題を解決できたと言いたかっただけです。また、C#プロジェクトのビルドをPlatform Target x64に設定して、64ビットのレジストリ領域を検索するためのエラーを回避できることにも気付きました。ただし、私のアプリケーションには、任意の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);
}
}
}