-1

I checked in the internet for getting the path of an executable file that is run. I found the answer:

string executingApplicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

which returns the path of the C# application I am running. I was unable to find a way of returning the path of an executable file that I am running using the Process Class:

Process.Start("runningExecutableFile.exe", "arguments");

the C# executable file that runs the "runningExecutableFile.exe" is located in the directory of this file. I need exactly this directory. The problem is that this "runningExecutableFile.exe" may be situated in different directories and I just want to copy the .exe file of my C# application to this directory and find programmatically the path in which the file is located.
Please help.

user2102327
  • 59
  • 2
  • 6
  • 19
  • 2
    `processObj.MainModule.FileName` is the executables path. – Alex K. Sep 21 '17 at 15:34
  • Have you reviewed https://stackoverflow.com/questions/25604475/getmodulefilenameex-function-returns-wrong-path-of-system-processes ? – kvr Sep 21 '17 at 15:35
  • you want runningExecutableFile.exe to sense its own path when it is called like you show, or you want the calling process, which is also a managed executable under your control, to find the absolute path of `runningExecutableFile.exe` from nothing than the current directory where the calling image is running from? – Cee McSharpface Sep 21 '17 at 16:17
  • @Alex K Thank you very much. It works like a charm. – user2102327 Sep 22 '17 at 04:57

1 Answers1

0

To achieve what you are looking for you could use the System.Diagnostics library as following:

using System.Diagnostics;

namespace YourNameSpace
{
    class Program
    {
        public static void Main()
        {
            Process[] pcs = Process.GetProcessesByName("YourProcessName");
            string path = pcs[0].MainModule.FileName;
        }
   }
}

.GetProcessesByName("..") will return an array of processes which will contain your desired process. To get the desired path you could just use the .MainModule.FileName properties.

oRole
  • 1,316
  • 1
  • 8
  • 24