【问题标题】:Needing help executing a windows form C# app off a thumb drive需要帮助从拇指驱动器上执行 Windows 窗体 C# 应用程序
【发布时间】:2018-12-10 09:06:43
【问题描述】:

我是 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();
    }

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

【问题讨论】:

  • 如果您将程序编码为始终查找该路径,那么您将无法在不重新编译代码的情况下解决问题。您需要使用Configuration 文件来解决这些问题
  • 将资源编译到可执行映像中。似乎没有立即需要将它们放在磁盘上。除非这些是第 3 方资产。在这种情况下,我们无法提出任何建议,因为我们无法知道他们的居住地。

标签: c# windows forms path parent


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    然后只需添加文件的路径:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多