2012-08-24 16 views
9

ho questo semplice linea di codice:FileNotFoundException a System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()

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

In realtà, il percorso è alimentato dalla linea di comando. Questa semplice App Console è stata compilata per i test e nei miei test trovo che:

  • Connessione al mio PC Windows 7 funziona.
  • Connessione a qualsiasi altra macchina Windows XP sulla rete, funziona.
  • Collegamento a qualsiasi altro di Windows 7 macchine sulla rete viene a mancare con:

Eccezione non gestita: System.IO.FileNotFoundException: Il percorso di rete non è stato trovato.

a System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo() a System.DirectoryServices.DirectoryEntry.RefreshCache() a System.DirectoryServices.DirectoryEntry.FillCache (String propertyName) a System.DirectoryServices.DirectoryEntry. get_NativeGuid() a System.DirectoryServices.DirectoryEntry.get_Guid() a GetDirectoryEntryProperties.Program.Main (String [] args) in D: \ GetDirectoryEntryProperties \ Program.cs: linea 15

Tutte le idee?

Sono un amministratore su tutte le macchine, tuttavia ho avuto un altro problema causato da un servizio di blocco dispositivo che ha causato un interrogazione UnauthorizedAccessException, ma in questo caso non riesco nemmeno a leggere il Guid della macchina.

Il registro eventi non mostra nulla di utile.

Luca

+0

Penso che avete bisogno di cambiare questo 'WinNT: //' per Windows 7 macchine. questo è un colpo nel buio Guess. – Malachi

+0

Ho appena creato uno script VBS usando GetObject e il provider WinNT e sto leggendo bene il Guid così qualcosa.NET non gli piace. Penso che .NET imponga un RefreshCache su tutte le proprietà mentre il modo COM non lo fa. Potrei forzarlo da VBS e vedere se emergono errori decenti. –

+0

puoi mostrare altro codice? l'ultima riga del tuo errore fa riferimento a 'D: \ GetDirectoryEnttryProperties \ Program.cs: riga 15' e' .cs' è l'estensione del file per 'C#' File ??? – Malachi

risposta

13

mi sono imbattuto lo stesso messaggio di errore per una situazione diversa. Forse la soluzione che ho trovato potrebbe aiutarti anche tu.

Dopo l'aggiornamento a Windows 10, il mio computer ha emesso un errore popup durante l'avvio che sembrava proprio come quello che hai postato. Era una FileNotFoundException su System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo().

La soluzione era copiare due stringhe da una posizione del registro a un'altra.

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

Grazie Bryan per essere tornato. Non ci sto lavorando più, ma potrei tornare un giorno in quell'area. –

+0

Grazie Bryan, questo ha funzionato per me. (Vinci 10 aggiornamenti più volte). Stavo aprendo la connessione con il nuovo PrincipalContext (ContextType.Machine, Environment.MachineName)) e ho ottenuto il FileNotFoundException quando si chiama FindAll() – tburi

+0

Contrassegnato come risposta in base al commento di tburi. –

0

Volevo solo dire grazie a Bryan Roach che ho potuto risolvere i miei problemi. Mi rendo anche conto che potrei impostare il mio progetto C# Build to Platform Target x64 ed evitare l'errore poiché cercherebbe l'area del registro a 64 bit. Tuttavia, ritengo sia più appropriato che la mia applicazione sia qualsiasi CPU e il programma stesso sia in grado di risolvere il problema.

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 
     { } 
    } 
} 

Poi per la funzione per copiare chiavi di registro al posto giusto:

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); 
     } 
    } 
} 
Problemi correlati