Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find which drive my exe is running from?

Tags:

c#

I am writing a c# app that needs to be able to be ran from a usb, and it launches other programs that are installed to the usb. Not necessarily the computer it's being ran on. I am launching these applications by doing:

Process.Start("notmyprogram.exe");

this works great however I am finding that this only works if notmyprogram.exe. is installed to the computers hard drive. If I want to run a program not installed on the computer, but rather to the usb, I have to call it directly, like so:

Process.Start("D:\\Programs\\notmyprogram\\applicationinstalledonusb.exe");

Where D: is the letter assigned by my OS. However, obviously the drive letter changes per computer, it wont always be D. MY application is located on the usb so my question is: is there any way to find what drive letter my exe is ran from so I could then append the directory to the end of the drive letter? Or maybe I'm simply going about it the wrong way and there's a more efficient way to accomplish this?

like image 833
Halter Avatar asked Jan 10 '14 18:01

Halter


1 Answers

Try this (using System.IO):

string root = Path.GetPathRoot(System.Reflection.Assembly.GetEntryAssembly().Location);

Also, You can use Path.GetFullPath() instead Path.GetPathRoot() and remove unwanted part from the string without hardcoding the folder name

like image 51
Kaf Avatar answered Oct 26 '22 23:10

Kaf