TheSwamp

Code Red => .NET => Topic started by: Fred Tomke on May 13, 2011, 07:36:06 AM

Title: Acad executable path
Post by: Fred Tomke on May 13, 2011, 07:36:06 AM
Hello,

is there a way to get the path of the running AutoCAD executable without using COM (I'm not allowed to have a link to the Type Library). I don't want to believe that there is no string property with the executable.

way 1:
Code: [Select]
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().Location);
way 2:
Code: [Select]
using System;
using System.Diagnostics;
using Autodesk.AutoCAD.ApplicationServices;

namespace AcadApp
{
    public static string GetAcadExec()
    {
      IntPtr AcadPtr = Application.MainWindow.Handle;
      Process[] arrProcs = Process.GetProcesses();
      Process oProc = null;
      string strPath = null;
      foreach (Process p in arrProcs)
      {
        IntPtr nHandle = IntPtr.Zero;
        try
        {
          nHandle = p.MainWindowHandle;
        }
        catch
        { }
        if (nHandle != IntPtr.Zero && nHandle == AcadPtr)
        {
          oProc = p;
          break;
        }
      }
      if (oProc != null)
        strPath = System.IO.Path.GetDirectoryName(oProc.MainModule.FileName);
      return strPath;
    }
}

Isn't there any property to ask for the path of acad.exe?

Thanks, Fred
Title: Re: Acad executable path
Post by: jgr on May 13, 2011, 08:10:14 AM
Another way:
Code: [Select]
RegistryKey hive  = Registry.LocalMachine;
RegistryKey ack = hive.OpenSubKey(HostApplicationServices.Current.RegistryProductRootKey);
string location = ack.GetValue("AcadLocation") as string;
Title: Re: Acad executable path
Post by: Jeff H on May 13, 2011, 04:31:53 PM
Does this help?

http://www.theswamp.org/index.php?topic=36234.msg412966#msg412966
Title: Re: Acad executable path
Post by: Fred Tomke on May 16, 2011, 02:20:03 AM
Hello,

thank you for your replies.
Obviously there is no property for that.

@jgr: I already had an eye on the registry key but I was afraid of the case somebody has changed or deleted the value.

@Jeff: Thanks for the link. At least it will shorten my second way. I must have in mind that it could be the case that different AutoCAD releases are currently running.  Comparing the handles of the Process.MainWindow will be the key to get the right AutoCAD instance.

Have a nice week.
Fred