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

I am writing a C # application that should be launched from a USB drive, and runs other programs that are installed in usb. Not necessarily the computer on which he was running. I launch these applications by following these steps:

Process.Start("notmyprogram.exe"); 

this works fine, but I find that it works only if notmyprogram.exe. installed on your computerโ€™s hard drive. If I want to run a program that is not installed on the computer, but rather on usb, I must call it directly, for example:

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

Where D: is the letter assigned to my OS. However, it is obvious that the drive letter is changed to one computer, usually it will not be D. MY application is located on usb, so my question is: is there a way to find which drive letter my exe is running on, so I could add a directory to the end of the letter drive? Or maybe I'm just wrong, and is there a more efficient way to do this?

+6
source share
1 answer

Try this (using System.IO):

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

Alternatively, you can use Path.GetFullPath () instead of Path.GetPathRoot() and remove the unnecessary part from the string without hardcoding the folder name

+7
source

All Articles