This project is read-only.

Finding the Search Path via Windows Registry

If R is installed with the option of ‘Save version number in registry', you may find the directory in which the R DLLs locate.

Snippet
Microsoft.Win32.RegistryKey rCore = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\R-core");
if (rCore == null)
{
   throw new System.ApplicationException("Registry key is not found.");
}
bool is64Bit = System.Environment.Is64BitProcess;
Microsoft.Win32.RegistryKey r = rCore.OpenSubKey(is64Bit ? "R64" : "R");
if (r == null)
{
   throw new System.ApplicationException("Registry key is not found.");
}
System.Version currentVersion = new System.Version((string)r.GetValue("Current Version"));
string installPath = (string)r.GetValue("InstallPath");
string bin = System.IO.Path.Combine(installPath, "bin");
// Up to 2.11.x, DLLs are installed in R_HOME\bin.
// From 2.12.0, DLLs are installed in the one level deeper directory.
return currentVersion < new System.Version(2, 12) ? bin : System.IO.Path.Combine(bin, is64Bit ? "x64" : "i386");

match Microsoft.Win32.Registry.LocalMachine.OpenSubKey (@"SOFTWARE\R-core") with
| null -> System.ApplicationException("Registry key is not found.") |> raise
| rCore ->
   let is64bit = System.Environment.Is64BitProcess
   match rCore.OpenSubKey (if is64bit then "R64" else "R") with
   | null -> System.ApplicationException("Registry key is not found.") |> raise
   | r ->
      let getString key = r.GetValue (key) :?> string
      let (%%) dir name = System.IO.Path.Combine (dir, name) 
      let currentVersion = System.Version (getString "Current Version")
      let binPath = getString "InstallPath" %% "bin"
      if currentVersion < System.Version (2, 12) then
         binPath
      else
         binPath %% if is64bit then "x64" else "i386"

Reference:

Last edited Aug 11, 2013 at 5:36 PM by kos59125, version 4

Comments

No comments yet.