0

I have published a WPF Application and added a File Association. When I click on my custom file *.bms I actually want to read the file and work with it. How can I do this? So far, if I click on a *.bms file it just opens the Application. Nothing else. I have already tried adding:

<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>

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
        }
    }
}

I got this solution from an answer by neeKo in This Question

But this didn't work for me neither.

Paul Karam
  • 4,052
  • 8
  • 30
  • 53
USAfirefighter
  • 75
  • 3
  • 10
  • Are you getting any exception? Maybe not accessible file? – Ferus7 Dec 14 '17 at 08:13
  • the answer you linked to is correct. You need to look at the passed arguments to Main and handle the FileLoad from there. What is the problem you are facing when trying to use it? – NoviceProgrammer Dec 14 '17 at 08:22
  • Did you try to remove the App from windows (right click on it) and relaunch it? – rodrigogq Dec 14 '17 at 08:22
  • @Ferus7 I don't get an exception. It does not jump into the if(e.Args.Length ==1) – USAfirefighter Dec 14 '17 at 09:18
  • @NoviceProgrammer It does not jump into the if(e.Args.Length ==1) when I open my custom file. – USAfirefighter Dec 14 '17 at 09:20
  • @USAfirefighter maybe sounds obvious but, is surely your app name: `WpfApplication1.App`? – Ferus7 Dec 14 '17 at 09:53
  • `and added a File Association` where and how? File associations aren't magic. There's a registry key that specifies what to call when a file is clicked and a specific command selected, like Open (the double click), Print etc. – Panagiotis Kanavos Dec 14 '17 at 10:03
  • @Ferus7 No it is not, it is just the example code from the other question. My code is set-up with the right name of course. I only added the line "Startup="Application_Startup" to my App.xaml. – USAfirefighter Dec 14 '17 at 10:05
  • @PanagiotisKanavos I published it and under Properties - > Publish -> Options -> File Association I created the Fire Association – USAfirefighter Dec 14 '17 at 10:06
  • @USAfirefighter *how* did you set up the association? If *your* registry settings don't pass the file path, your application won't get it. If you set the association properly, the file will always be available in the command line – Panagiotis Kanavos Dec 14 '17 at 10:06
  • @PanagiotisKanavos when I publish it through VS - where can I say that it also should add a key to the registry so it will pass a path when I double-click my file? – USAfirefighter Dec 14 '17 at 10:09
  • @USAfirefighter "Publish" doesn't *install* anything, it creates a ClickOnce deployment package. When you install the package, the appropriate registry settings are created. You should investigate what's wrong with the association. – Panagiotis Kanavos Dec 14 '17 at 10:09
  • @PanagiotisKanavos True, after publish I install my app through the created setup.exe. Once installed my Computer is recognizing my .bms files and adds my custom icon and on double click it opens my WPF Application. – USAfirefighter Dec 14 '17 at 10:16
  • [This](https://stackoverflow.com/questions/4233915/wpf-click-once-double-click-file-to-launch-vs-2008) has helped me and made it work. Thank you for your help guys! – USAfirefighter Dec 14 '17 at 13:49

0 Answers0