2012-08-24 83 views
9

我的代碼這個簡單的行:FileNotFoundException異常在System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()

var entry = new DirectoryEntry("WinNT://DOMAIN/MachineName, Computer"); 
Console.WriteLine(entry.Guid); 

在現實中,路徑由命令行供給。這個簡單的控制檯應用程序編譯進行測試,並在我的測試中發現:

  • 連接到我自己的Windows 7 PC的作品。
  • 連接到網絡上的任何其他Windows XP計算機均可正常工作。
  • 連接到網絡上的任何其他Windows 7計算機失敗:

未處理的異常:System.IO.FileNotFoundException:找不到網絡路徑沒有被發現。

在System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() 在System.DirectoryServices.DirectoryEntry.RefreshCache() 在System.DirectoryServices.DirectoryEntry.FillCache(字符串propertyName的) 在System.DirectoryServices.DirectoryEntry。 get_NativeGuid() 在System.DirectoryServices.DirectoryEntry.get_Guid() 在GetDirectoryEntryProperties.Program.Main(字串[] args)在d:\ GetDirectoryEntryProperties \ Program.cs的:線15

任何想法?

我是所有機器上的管理員,但是我確實有另外一個設備鎖定服務導致UnauthorizedAccessException審訊問題,但在這種情況下,我甚至無法讀取機器的Guid。

事件日誌顯示沒有任何用處。

Luke

+0

我認爲你需要改變這種'WINNT://'的Windows 7機器。這是在黑暗猜猜中拍攝的。 – Malachi

+0

我只是做了在將GetObject和WinNT提供VBS腳本這很好讀的GUID這樣的東西。NET它不喜歡。我認爲.NET在所有屬性上強制使用RefreshCache,而COM方式則不然。我可能會從VBS強制它,看看是否有任何體面的錯誤出現。 –

+0

你能展示一些更多的代碼嗎?錯誤的最後一行引用'D:\ GetDirectoryEnttryProperties \ Program.cs:第15行和'.cs'是'C#'文件的文件擴展名? – Malachi

回答

13

我遇到了不同情況的同一個錯誤消息。也許我找到的解決方案也可以幫助你。

升級到Windows 10後,我的電腦在啓動時出現彈出錯誤,看起來就像您發佈的那個。它是System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()中的FileNotFoundException。

解決的辦法是兩個字符串從一個註冊表位置複製到另一個。

Copy these strings: RegisteredOwner and RegisteredOrganization 

From: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion 

To: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion 
+1

感謝Bryan回來發帖。我不再工作了,但我可能有一天會回到那個領域。 –

+0

謝謝Bryan,這對我很有用。 (Win 10多次升級)。我打開與新的PrincipalContext(ContextType.Machine,Environment.MachineName))的連接,並根據tburi的評論調用FindAll() – tburi

+0

標記爲答案時,我得到FileNotFoundException。 –

0

我只想說感謝布賴恩羅奇我能解決我的問題。我也知道我可以把我的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); 
     } 
    } 
} 
相關問題