5

Possible Duplicate:
How to associate a file extension to the current executable in C#

So, I'm making an application for school (final project).

In this application, I have a Project-class. This can be saved as a custom file, e.g. Test.gpr. (.gpr is the extension).

How can I let windows/my application associate the .gpr file with this application, so that if I doubleclick the .gpr file, my application fires and opens the file (so launches the OpenProject method - This loads the project).

I am NOT asking how to let windows associate a file type with an application, I am asking how to catch this in my code in Visual Studio 2012.

UPDATE: Since my question seems to be not so clear:

atm, I've done nothing, so I can follow whatever is the best solution. All I want is to doubleclick the .gpr, make sure windows knows to open it with my app, and catch the filepath in my application.

Any help is greatly appreciated!

Community
  • 1
  • 1
voluminat0
  • 866
  • 2
  • 11
  • 21
  • 1
    If you associate an extension with your application, then the filename is passed as the first parameter (command line) to your main/WinMain function when the user double-clicks a file of that type. – Cameron Nov 30 '12 at 22:41
  • @Cameron, you mean something like Form(string path-to-.gprfile) as some sort of constructor? – voluminat0 Nov 30 '12 at 22:42
  • Windows needs to be able to know to run your application in the first place when you doubleclick the file. And the way to do that is through a file association, which you seem to not want to do. Are you saying your application will already be running and detect the file open attempt by Explorer? – itsme86 Nov 30 '12 at 22:44
  • 4
    He means that in your `static void Main(string[] args)`, `args[0]` will be the path to the file. – neeKo Nov 30 '12 at 22:45
  • @Valentijn: No, sorry (I thought this was a C++ question for some reason). Niko clarified what I meant (thanks @Niko!). – Cameron Nov 30 '12 at 22:49
  • @itsme86, atm, I've done nothing, so I can follow whatever is the best solution. All I want is to doubleclick the .gpr, make sure windows knows to open it with my app, and catch the filepath in my application. – voluminat0 Nov 30 '12 at 22:49

1 Answers1

12

When you open a file with an application, the path to that file is passed as the first command line argument.

In C#, this is args[0] of your Main method.

static void Main(string[] args)
{
    if(args.Length == 1) //make sure an argument is passed
    {
        FileInfo file = new FileInfo(args[0]);
        if(file.Exists) //make sure it's actually a file
        {
           //Do whatever
        }
    }

    //...
}

WPF

In case your project is an WPF application, in your App.xaml add a Startup event handler:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Startup="Application_Startup"> <!--this line added-->
    <Application.Resources>

    </Application.Resources>
</Application>

Your command line arguments will now be in e.Args of the Application_Startup event handler:

private void Application_Startup(object sender, StartupEventArgs e)
{
    if(e.Args.Length == 1) //make sure an argument is passed
    {
        FileInfo file = new FileInfo(e.Args[0]);
        if(file.Exists) //make sure it's actually a file
        {
           //Do whatever
        }
    }
}
neeKo
  • 4,280
  • 23
  • 31