需要帮助从拇指驱动器执行 windows 表单 C# 应用程序

Needing help executing a windows form C# app off a thumb drive

我是 C# 的新手,无法通过拇指驱动器执行我的应用程序。我电脑上的代码如下

    private void buttonanticipation_Click(object sender, 
    EventArgs e)
    {
        SoundPlayer sPlayer = new SoundPlayer (@"C:\Users\MyPC\SoundBoardApp\Soundboards\AnticipationSound.wav");
            sPlayer.Play();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void buttonrecordscreech_Click(object sender, EventArgs e)
    {
        SoundPlayer sPlayer = new SoundPlayer(@"C:\Users\MyPC\SoundBoardApp\Soundboards\RecordScreetch.wav");
        sPlayer.Play();
    }

但我需要它做的就是找到闪存驱动器上指定的路径。在我的电脑上,路径是 D: 但在其他电脑上会有所不同。任何帮助将不胜感激。

您可以将 soundboards 文件夹放在您的 .exe 所在的位置。然后,获取exe目录的路径:

string path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

然后只需添加文件路径即可:

path += "/Soundboards/RecordScreetch.wav";

如果文件位于应用程序文件夹中的某个位置,您可以使用 System.Windows.Forms.Application.StartupPath

string path = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Soundboards\RecordScreetch.wav");
SoundPlayer sPlayer = new SoundPlayer(path);