3

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
}
Sentry
  • 4,102
  • 2
  • 30
  • 38
dvjanm
  • 2,351
  • 1
  • 28
  • 42

2 Answers2

2

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

Samich
  • 29,157
  • 6
  • 68
  • 77
  • That is not working for me, e.Args is empty. I was able to get in another way:AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0] – dvjanm Jul 28 '17 at 06:58
0

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;
dvjanm
  • 2,351
  • 1
  • 28
  • 42