使用 WMPLib 定位音频文件时出现问题

Trouble pathing an audio file using WMPLib

我是 C# 的新手,路径文件困扰了我一段时间。

音频文件的文件结构如下:

C:\Users\Username\Documents\Program\Form\WindowsFormApp\Audio Files\name.mp3

我的 visual studio 表单位于:

C:\Users\Username\Documents\Program\Form\WindowsFormApp

我尝试使用以下方法引用音频文件 URL:

soundplayer.URL = @"Audio Files\name.mp3";

然而,这并没有奏效。

然后我将文件夹 Audio Files 放入几乎所有其他文件夹中。还是不行。

我试过了:

soundplayer.URL = @"..\Audio Files\name.mp3";

那也不行。我不能简单地完成音频的整个路径,因为它在每台计算机上都不同。

如何正确设置路径?

Well, in your case, it would be as follows

using System.IO;
using WMPLib;

WindowsMediaPlayer player;
private void button1_Click(object sender, EventArgs e)
{
 string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Audio Files\name.mp3");
 player = new WindowsMediaPlayer();
 FileInfo fileInfo = new FileInfo(path);
 player.URL = fileInfo.Name;
 player.controls.play();
}

在哪里

//This function gets the current route of your project and combines it with the subfolder path where your music file is
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Audio Files\name.mp3");