dvjanm
dvjanm

Reputation: 2381

Get file that the user opened the application with

I have a WPF application. I associated a file extension with my app. Now I can open the app by double-clicking on a file with this extension.

My problem is that I do not know how to get the file the user opened.

The following is not working:

Environment.GetCommandLineArgs() only contains 1 element with the app name.

private void Application_Startup(object sender, StartupEventArgs e)
{
    //e.Args is empty
}

Upvotes: 3

Views: 165

Answers (2)

dvjanm
dvjanm

Reputation: 2381

I solved the problem. I can get the file the following way:

fname = AppDomain.CurrentDomain.SetupInformation
        .ActivationArguments.ActivationData[0];
Uri uri = new Uri(fname);
fname = uri.LocalPath;

Upvotes: 0

Samich
Samich

Reputation: 30175

In you App.xaml.cs file override handler for Startup event:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        e.Args[0] // here's your file name
        base.OnStartup(e);
    }
}

You can grab value of your file name from startup argument in it. You also might need to check your association since what you have should work and you should be able to access you file name as second param

dubugger-with-resuts

Upvotes: 1

Related Questions