如何播放应用程序文件夹中的声音?

How can I play a sound that will be in the application folder?

我试图在程序初始化时播放声音,但当我将安装程序传递给其他人时它不起作用,因为目标在他们的计算机中不正确。

我怎样才能以在其他计算机上工作的方式引用该文件?

这是我使用的代码:

public partial class Form1 : Form
{
    private SoundPlayer unacabeza = new SoundPlayer();

    public Form1()
    {
        InitializeComponent();
        unacabeza.SoundLocation = @"c:\Por_una_Cabeza_-_Carlos_Gardel_Gcxv7i02lXc.wav";
        unacabeza.Play();
    }
}

您可以将 wav 文件放在执行文件夹中,然后只用 unacabeza.SoundLocation = @"Por_una_Cabeza_-_Carlos_Gardel_Gcxv7i02lXc.wav";

替换您的代码

您可以将 wav 文件放在 Resources 文件夹中 当你这样做时,你不必担心用户删除的位置或文件,你可以通过

解决方案探索 > 属性 > 双击资源 然后将打开一个新选项卡,将您的 wav 文件拖放到其中

并重建项目

播放来自资源的wav文件,您只需要

SoundPlayer test= new SoundPlayer(Properties.Resources.'filename');
test.play();

如果您只需要从应用程序文件夹中播放 wav,您可以

  • SoundPlayer test = new SoundPlayer(Application.StartupPath + "\filename.wav");
  • SoundPlayer test = new SoundPlayer(".\filename.wav");