An easy solution is to use the WPF MediaPlayer
control in code,
https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.mediaplayer?view=windowsdesktop-6.0
Step 1: Open your project file and enable WPF,
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Step 2: Initialize the control in code and play some file,
using System.Windows.Media;
namespace testwinforms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var player = new MediaPlayer();
player.Open(new Uri(@"C:\Users\someuser\Downloads\somefile.mp3"));
player.Play();
}
}
}
This works for .NET Core 3.0/3.1 and .NET 5/6.