Author Topic: Acad executable path  (Read 2784 times)

0 Members and 1 Guest are viewing this topic.

Fred Tomke

  • Newt
  • Posts: 38
  • [ Mr. Bad Guy ]
Acad executable path
« 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
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]

jgr

  • Guest
Re: Acad executable path
« Reply #1 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;

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Acad executable path
« Reply #2 on: May 13, 2011, 04:31:53 PM »

Fred Tomke

  • Newt
  • Posts: 38
  • [ Mr. Bad Guy ]
Re: Acad executable path
« Reply #3 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
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]