0

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

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

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

我的视觉工作室表格位于:

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

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

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

然而,这并没有奏效。

然后我将文件夹 Audio Files 放在几乎所有其他文件夹中。它仍然没有工作。

我试过了:

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

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

我该如何正确路径?

4

1 回答 1

2

好吧,在你的情况下,它将如下

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");
于 2018-03-19T07:09:04.540 回答