0

I'm trying to Create an installer for my CAD plugin, and need to get the AutoCAD install location. but the return values of RegistryKey.GetSubKeyNames() is different from what I see in Registry Editor.

string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
    foreach (string subkey_name in key.GetSubKeyNames())
    {
        Console.WriteLine(subkey_name);
    }
}

output:

AddressBook
Autodesk Application Manager
Autodesk Content Service
Autodesk Material Library 2015
Autodesk Material Library Base Resolution Image Library 2015
Connection Manager
DirectDrawEx
DXM_Runtime
f528b707
Fontcore
...

In Registry Editor:

animizvideocn_is1
AutoCAD 2015
Autodesk 360
Connection Manager
...

AutoCAD 2015 is what i need

lymims
  • 23
  • 1
  • 7

2 Answers2

0

Your installer seems to be a 32 bit application, or at least runs as a 32 bit process.

Therefore, Windows redirects

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

to

HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall

To access the non redirected node, follow the instructions here.

Heinz Kessler
  • 1,610
  • 11
  • 24
  • Thank you, Heinz Kessler, that helps a lot ! after replacing `Registry.LocalMachine` with `RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)`, I finally got *AutoCAD 2015* in the return values. – lymims May 31 '19 at 02:00
0

This might be not a direct answer to your question, but i had to do the same thing. I was not looking at the registry, but the Program Files directory. It will then add the netload command to the autoload lisp file. It will install a list of Plugin dlls to all installed autocad versions. This can easily be changed... Hopefully it helps.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace AMU.AutoCAD.Update
{
  public class AutoCadPluginInstaller
  {
    private static readonly Regex AutoloadFilenameRegex = new Regex("acad([\\d])*.lsp");

    public void Install(IEnumerable<string> pluginFiles)
    {
      var acadDirs = this.GetAcadInstallationPaths();
      var autoloadFiles = acadDirs.Select(this.GetAutoloadFile);

      foreach (var autoloadFile in autoloadFiles)
        this.InstallIntoAutoloadFile(autoloadFile, pluginFiles);
    }

    private void InstallIntoAutoloadFile(string autoloadFile, IEnumerable<string> pluginFiles)
    {
      try
      {
        var content = File.ReadAllLines(autoloadFile).ToList();
        foreach (var pluginFile in pluginFiles)
        {
          var loadLine = this.BuildLoadLine(pluginFile);
          if(!content.Contains(loadLine))
            content.Add(loadLine);
        }

        File.WriteAllLines(autoloadFile, content);
      }
      catch (Exception ex)
      {
        //log.Log();
      }
    }

    private string BuildLoadLine(string pluginFile)
    {
      pluginFile = pluginFile.Replace(@"\", "/");
      return $"(command \"_netload\" \"{pluginFile}\")";
    }

    private IEnumerable<string> GetAcadInstallationPaths()
    {
      var programDirs =
        Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

      var autoDeskDir = Path.Combine(programDirs, "Autodesk");
      if (!Directory.Exists(autoDeskDir))
        return null;

      return Directory.EnumerateDirectories(autoDeskDir)
                             .Where(d => d.Contains("AutoCAD"));
    }

    private string GetAutoloadFile(string acadDir)
    {
      var supportDir = Path.Combine(acadDir, "Support");
      var supportFiles = Directory.EnumerateFiles(supportDir);
      return supportFiles.FirstOrDefault(this.IsSupportFile);
    }

    private bool IsSupportFile(string path)
      => AutoloadFilenameRegex.IsMatch(Path.GetFileName(path));
  }
}

(see here: https://gist.github.com/felixalmesberger/4ff8ed27f66f872face4368a13123fff)

You can use it like this:

var installer = new AutoCadPluginInstaller();
installer.Install(new [] {"Path to dll"});

Have fun.

  • thanks for your reply, but some of our users may not install the AutoCAD in Program Files directory, that's why I'm trying to get the app list from registry. – lymims May 31 '19 at 01:37