Click here to Skip to main content
16,022,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a media player in WPF using the MediaElement, that will play media if selected in the player through an openfiledialog box. This works fine.

But how can I get the media to play if I right click on any media file, select 'Open With' and browse to the mediaplayer.exe file that I created.

When I select mediaplayer.exe from 'Open With' , it will open the player, but doesn't load the video.

What I have tried:

xaml for the media element

<MediaElement  Grid.Row="0" Name="mediaPlayer"  Stretch="Uniform" Loaded="mediaElement_Loaded" LoadedBehavior="Manual"/>


the code behind function

private void mediaElement_Loaded(object sender, RoutedEventArgs e)
{
    mediaPlayer.Play();
}


This works when using the openfiledialog method, but not 'Open With'
Posted

The path to the file is passed as an argument to your executable when you use Open with. Use that argument to populate the media player and run from that.
 
Share this answer
 
Your app has to handle command line arguments. When you use Open With, Explorer will launch your app with the following command line:
mediaplayer.exe "Full\Path\To\The\Media\File.ext"

Google for "c# wpf handle command line arguments" and you'll find several ways of accomplishing the task.
 
Share this answer
 
Well, it took me around 8 hours but I finally figured it out.

I had to change App.xaml.cs to:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    //-- e.Args returns the file path of what is right clicked on 
    //-- that you want to "Open With"
    if (e.Args.Length > 0)
    {
        //-- message boxes so I know what part of the code I'm in at runtime
        MessageBox.Show(e.Args[0]);
        MessageBox.Show("In the if statement");

        MainWindow mw = new MainWindow(e.Args);
        mw.Show();
    }
    else
    {
        MessageBox.Show("In the Else statement");

        MainWindow mw = new MainWindow();
        mw.Show();
    }


I had to make another MainWindow constructor to use for "Open With".
If I just open the program normally then I use the constructor with the empty parameters.

In the MainWindow.xaml.cs I added a constructor that took a string[] argument.

public MainWindow(string[] args)
{
    //-- verify the argument is actually passed
    MessageBox.Show(args[0]);

    //-- add the path to the media element
    mediaplayer.Source = new Uri(args[0]);
 
Share this answer
 
Comments
Chris Copeland 26-Jun-24 5:16am    
So.. you implemented what was recommended to you in the other two solutions, then many hours later decide to post your own solution and accept that without giving credit to the others? Both Pete and Dave should have had their solutions accepted.
Da Man 2024 26-Jun-24 14:35pm    
I accepted my answer as correct because it actually shows how to solve the problem. If I would have been able to solve it right away from their responses, I would have gave credit. Anyone else that reads their answers will also have to take a certain amount of time to go elsewhere and figure it out.
They could have written some code, but chose not to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900