Nel caso abbiate necessita' di determinare la directory di installazione e la versione del .NET framework che la vostra applicazione sta utilizzando potete utilizzare le funzioni GetCORVersion e GetCORSystemDirectory presenti nella libreria mscoree.dll. Il codice che segue wrappa tali chiamate in una classe CLRInfo in modo che da semplificarne l'uso.
public class CLRInfos{
//API declares
[DllImport("mscoree.dll")] static extern Int32 GetCORSystemDirectory ([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder buffer,Int32 buflen, ref Int32 numbytes);
[DllImport("mscoree.dll")] static extern Int32 GetCORVersion ([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder buffer,Int32 buflen, ref Int32 numbytes);
static public String SystemDirectory(){
System.Text.StringBuilder buf=new System.Text.StringBuilder(1024);
Int32 iBytes=0;
Int32 ret= GetCORSystemDirectory(buf,buf.Capacity, ref iBytes);
return buf.ToString().Substring(0,iBytes-1);
}
public static string Version(){
System.Text.StringBuilder buf = new System.Text.StringBuilder(1024);
Int32 iBytes=0;
Int32 ret= GetCORVersion(buf,buf.Capacity, ref iBytes);
return buf.ToString().Substring(0,iBytes-1);
}
}
Esempio d'uso:
String dir=CLRInfos.SystemDirectory();
String ver=CLRInfos.Version();
Un esempio pratico d'uso di queste funzioni potrebbe essere all'interno di una custom action in modo da poter individuare NGen.exe e compilare le vostre assemblies durante l'installazione del programma.