Ok, you know how in programs like Microsoft Excel, or Adobe Acrobat Reader you can click on a file in explorer and it will open with the associated program. That's what I want my application to do. Now, I know how to set up the file associations in Windows so that it knows the default program for each extension. My question is how do I get my application to open the file when I double click the file.
I've searched the web using google, I've searched the msdn site, and I've searched several forums including this one but I haven't found anything that explains how to accomplish this. I'm guessing it has something to do with the parameters of the main method but that's just a guess.
If someone can point me in the right direction I can take it from there. Thanks in advance for your help.
Shane
Choose Start→All Programs. Click the program name on the All Programs list that appears. You see a list of programs; just click the program on that sublist to open it. Double-click a program shortcut icon on the desktop.
In File Explorer, right-click on a file whose default program you want to change. Select Open With > Choose Another App. Check the box that says “Always use this app to open . [file extension] files.” If the program you want to use is displayed, select it and click OK.
To see the open files for a process, select a process from the list, select the View->Lower Panel View->Handles menu option. All of the handles of type "File" are the open files. Also, a great way to find which application has a file open is by using the Find->Handle or DLL menu option.
Setting up the associations in windows will send the filename to your application on the command line.
You need to read the event args in your applications main
function in order to read the file path and be able to open it in your application.
See this and this to see how to access the command line arguments in your main
method.
static void Main(string[] args)
{
System.Console.WriteLine("Number of command line parameters = {0}", args.Length);
foreach (string s in args)
{
System.Console.WriteLine(s);
}
}
When you open the file, with associations set as you described, your application will be started with the first argument containing the filepath to your file.
You can try this out in a simple way by printing out the args
from your main
method, after you open your application by clicking on the associated file. The 0th element should be the path to your file.
Now, if you successfully reached this point, the all you need to do now is read the contents of the given file. I'm sure you'll find more than plenty of resources here on how to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With