简体   繁体   中英

Getting file that is launching exe

I have been looking around for the solution to my problem, and found nothing. How do you get the file that is being opened up by the default program that you set it to open up with? For example: if I have a file called HelloWorld.hello , and I set it up so that a program called Hello.exe will open when the file is clicked. How do I get the file that is launching Hello.exe ?

The problem is that calls to "execute HelloWorld.exe " go through a common methodology programmed into the OS - it's by definition an OS function to handdle execution requests. Thus, the most you'll ever get is your kernal for "what launched me?"

EDIT If you want to get the program that is executing current code, ie the entry point of an application, this is accessible via Environment.CommandLine . Unless you're building a library accessed by multiple executables, this is not very useful, and if you are, there are few cases where the library should know what's calling it.

If you have control of the calling function, you may want to consider passing in some arguments during startup into the targeted program - these are accessible via the string[] parameter in Main

EDIT Elaborating on the option to pass information into Main , you can actually have one of several signatures for your Main method. Using the following:

static void Main(string[] args)

Will take any command line arguments and pass them into the args array. Thus, if you called your program HelloWorld.exe (in windows) as

HelloWorld "alpha beta" delta gamma

then args will contains ["alpha beta", "delta", "gamma"] values. Thus, passing in flags or enabling command line arguments becomes possible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM