0

Hi I'm writing a simple WPF to show binary file content.

So I'd like to start the application when I try to open a file.

But I have no idea where to start.

How and where do I get the data of the file that is to be opened?

I tried reading commandline args from the

StartupEventArgs e

and from

Environment.GetCommandLineArgs();

bot return empty string arrays.

I can already process the file and open them during runtime but have no idea how to open one at start.

Does anyone have an idea where to start.

Thanks in advance

Stephan Pich
  • 171
  • 1
  • 2
  • 11
  • You can try adding a key to appSettings section of app.config file and read the key value which will be default location. I feel this is good and clean way. For more info on how to read, refer this link https://stackoverflow.com/questions/1189364/reading-settings-from-app-config-or-web-config-in-net – G K Oct 12 '19 at 10:42
  • You have to pass Arguments in in the Main Method in Program.cs to your Form. – Andreas Schmidt Oct 12 '19 at 10:44
  • Thats not what i mean my case is e.g. i have a file called temp.txt when i double click on it i want my wpf application to start. My question is what do i need to to the get information on this file to open it and where will i find it. I have no Programm.cs the lowest entrypoint i have is the private void Application_Startup(object sender, StartupEventArgs e) – Stephan Pich Oct 12 '19 at 10:54
  • Hi Stephan, do I get it right that you want to for example right-click a file and say "Open with" and chose your WPF application? And/Or you want to double click a file of a certain type that you associated with your application. And after either option it should open your application and do something with the extra info of the file you chose? – huserben Oct 12 '19 at 12:07
  • yes exactly theoretically i'd just need the path of the file to open but as i said i've no clue where to start – Stephan Pich Oct 12 '19 at 12:14

1 Answers1

2

When you select your WPF application in the File -> Open With dialog you will get it as parameter passed to your Application.

So if you're setting up your App.xaml like this:

<Application x:Class="WpfApp1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApp1"
         StartupUri="MainWindow.xaml"
         Startup="Application_Startup">
...
</Application>

You can listen in your App.xaml.cs for the Startup Event:

  private void Application_Startup(object sender, StartupEventArgs e)
  {
     foreach (var arg in e.Args)
     {
        MessageBox.Show(arg);
     }
  }

For demo purpose I just output all the arguments in a message box as it's hard to attach a debuger to the process in time. Now if we open any file with that app: Open File With Open with WPF App

We will see this:

Argument Message Box

So the file path will be passed in as the first parameter to the application. From there you can do with it whatever you need to.

If you would associate a filetype with your application you'll get the same behavior via double-clicking the file. If your application will be installed using an installed like Wix you would have the option to associate certain filetypes as part of the installation directly.

huserben
  • 1,034
  • 1
  • 10
  • 19