我只想說感謝布賴恩羅奇我能解決我的問題。我也知道我可以把我的C#項目建設到平臺目標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);
}
}
}
我認爲你需要改變這種'WINNT://'的Windows 7機器。這是在黑暗猜猜中拍攝的。 – Malachi
我只是做了在將GetObject和WinNT提供VBS腳本這很好讀的GUID這樣的東西。NET它不喜歡。我認爲.NET在所有屬性上強制使用RefreshCache,而COM方式則不然。我可能會從VBS強制它,看看是否有任何體面的錯誤出現。 –
你能展示一些更多的代碼嗎?錯誤的最後一行引用'D:\ GetDirectoryEnttryProperties \ Program.cs:第15行和'.cs'是'C#'文件的文件擴展名? – Malachi