Is it possible to run a software program (for example autocad) with C# without exact file path? [closed]

StackOverflow https://stackoverflow.com/questions/20442343

  •  30-08-2022
  •  | 
  •  

Question

I have the following code to run AutoCAD software :

string path = @"C:\Program Files\Autodesk\AutoCAD 2012 - English\acad.exe";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = path;
p.Start();

But with this code I need the exact file path of acad.exe

What I would like to be able to do is click the button and have my code find AutoCAD and run. But if it can not find , show a message.

Was it helpful?

Solution 2

I guess you need something like below;

using System;
using System.IO;

namespace FileLocator
{
 class Program
 {
  static void Main(string[] args)
  {
   string filename = "wmplayer.exe";// Only for example.Change to your exe's name.
   string searchFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86).Replace("Common Files", "");// ProgramFiles (x86) folder
   foreach (string directory in Directory.GetDirectories(searchFolder))// Iterate through each folder in PFx86.
   {
    foreach (string directoryfilename in Directory.GetFiles(directory, "*.exe"))// Get all files with .exe extension.
    {
     if (Path.GetFileName(directoryfilename) == filename)//If Current filename==wmplayer.exe
     {
      Console.WriteLine("Found {0} in {1}", filename, directory);
      Console.WriteLine("Complete Path => {0}", directoryfilename);
      Console.ReadLine();
     }
    }
   }
}
}
}

This method loops through each folder in destination for now its PFx86 to find out what we need.Be warned this technique can throw good number of Security exceptions.

OTHER TIPS

The AutoCAD.Application class is registered and localized to a path already. You can grab the path out of the registry and use that for your process start if you wanted to. Additionally you could use activator to create one from the ProgId, which would make more sense if you planned on interacting with AutoCAD at all.

   Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application"));

Look into the AutoCAD interop libraries on how to work with this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top