Created
June 27, 2025 16:37
-
-
Save 1jpg1/fcd7d87f987a6de74289fff2eab84f2f to your computer and use it in GitHub Desktop.
RevitInstallationLocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma warning disable CA1416 | |
using Microsoft.Win32; | |
var revitVersion = "2020"; | |
var revitPath = GetRevitPath(revitVersion); | |
Console.WriteLine($"Revit Path for {revitVersion} is {revitPath}"); | |
string? GetRevitPath(string version) | |
{ | |
try | |
{ | |
string baseKeyPath = $"SOFTWARE\\Autodesk\\Revit\\{version}"; | |
using (var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) | |
using (var rootKey = localMachine.OpenSubKey(baseKeyPath)) | |
{ | |
if (rootKey == null) | |
{ | |
Console.WriteLine($"Registry path not found: {baseKeyPath}"); | |
return null; | |
} | |
return FindInstallationPathRecursive(rootKey); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error while trying to get Revit {version} path: {ex.Message}"); | |
return null; | |
} | |
} | |
string? FindInstallationPathRecursive(RegistryKey key) | |
{ | |
try | |
{ | |
var installPath = key.GetValue("InstallationLocation") as string; | |
if (!string.IsNullOrEmpty(installPath)) | |
{ | |
var revitExePath = Path.Combine(installPath, "Revit.exe"); | |
if (File.Exists(revitExePath)) | |
return revitExePath; | |
} | |
foreach (var subKeyName in key.GetSubKeyNames()) | |
{ | |
using (var subKey = key.OpenSubKey(subKeyName)) | |
{ | |
if (subKey == null) | |
continue; | |
var result = FindInstallationPathRecursive(subKey); | |
if (!string.IsNullOrEmpty(result)) | |
return result; | |
} | |
} | |
return null; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error while searching registry: {ex.Message}"); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment