Reputation: 11
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
?
Upvotes: 0
Views: 50
Reputation: 10708
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
Upvotes: 1