How to play a music by click on it (wpf , c#)

IKINGSHADOW 136 Reputation points
2021-12-03T18:51:33.357+00:00

Hello everyone , So i have a music player that can find musics from the folders that the user added in the application and then the user can play them in the application but my question is , how can i play the musics just by click on them without my application running ( like windows music player ) , for example in windows 10 if you double click on a music file it will be play in groove , so how can i do that , ( i know that there are contents on the internet about this but I don't know what should i search about.) How can i play musics when the user double click on files in two situations , first when my application is ruining , second when my application isn't running ??

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

Accepted answer
  1. IKINGSHADOW 136 Reputation points
    2021-12-04T18:25:26.24+00:00

    I just found the solution for my problem by helping of other people in this community and stackoverflow ,
    Also this link provides the solution :
    https://stackoverflow.com/questions/58383027/how-to-start-my-program-and-read-text-file-by-clicking-the-file-from-desktop
    According to this solution thw windows will pass the address of the file through the application.startup , and we can retrieve the path from startupeventsargs.args ,
    Thank you everyone


2 additional answers

Sort by: Most helpful
  1. Rey Darifyi 6 Reputation points
    2022-09-02T12:02:54.84+00:00

    To get the opened file name from the Windows, use Environment.GetCommandLineArgs method. The Environment.GetCommandLineArgs method returns a string array containing the command-line arguments for the current process. The first element is the executable file name.

    To read file content, you could use System.IO.File.ReadAllText method:

    string FilePath = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).ToArray());  
    if (!string.IsNullOrEmpty(FilePath))  
    {  
       string FileContent = System.IO.File.ReadAllText(FilePath);  
       Console.WriteLine(FileContent);  
    }  
    

    237342-msft-music.png
    237309-msft-music.png

    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 122.8K Reputation points
    2021-12-03T19:10:10.04+00:00

    To play a file using your application, add a MediaElement control to your WPF Window using Designer. In XAML you will have something like '<MediaElement x:Name="mediaElement1" />'. To start playing a file, execute this line:

    mediaElement1.Source = new Uri( @"C:\MyFiles\SomeAudioFile.wma");  
    

    See some features: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-control-a-mediaelement-play-pause-stop-volume-and-speed.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.