Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a file with my application?

Tags:

c#-4.0

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

like image 306
Shane Herron Avatar asked Feb 05 '11 19:02

Shane Herron


People also ask

How do you open programs and application?

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.

How do I change a file that opens with default?

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.

What shows what file and applications are open?

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.


2 Answers

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);
    }
}
like image 110
Oded Avatar answered Sep 21 '22 06:09

Oded


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.

like image 23
Goran Jovic Avatar answered Sep 23 '22 06:09

Goran Jovic