2017-02-17 25 views
-5

所以我需要做简单的应用程序,将播放音乐按下按钮,但因为它要求我的路径设置为像这样的确切位置:player.SoundLocation = @“C:\用户\艾娃\桌面\新建文件夹(6)\ music.wav“;它只适用于我的电脑,所以如何使它可以在其他电脑上运行?没有确切的音乐文件位置?如果我想在其他电脑上使用应用程序,在哪里存储音乐文件?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace songs 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     System.Media.SoundPlayer player = new System.Media.SoundPlayer(); 
     if(button1.Text=="stop") 
     { 
      player.Stop(); 
      { 
       button1.Text = "play"; 

      } 
     } 
     else 
     { 
      player.SoundLocation = @"C:\Users\Ava\Desktop\New folder (6)\music.wav"; 
      player.Load(); 
      button1.Text = "stop"; 
      player.PlayLooping(); 
     } 

    } 
} 

}

+0

可以将您提供您究竟怎么回事这个例子吗?就目前而言,其答案只是使用相对路径而不是绝对路径。 – Eddge

+1

的可能的复制[如何获得Windows中的当前用户的主目录(http://stackoverflow.com/questions/9542611/how-to-get-the-current-users-home-directory-in-windows)。编辑:[本](http://stackoverflow.com/questions/1143706/getting-the-path-of-the-home-directory-in-c)是一个更好的例子。 –

+0

@Avadoto使用代码语法添加到您的问题。 – Eddge

回答

0

您可以使用相对路径来实现你想要的。最好的方式做到这一点,当然,是只具有用户输入的路径,但如果你真的想为你进行设置,相对路径是要走的路。

Here是别人关于将文件夹设置为用户特定路径的问题,至于相对路径,我不是很懂得知道如何去完成它,所以我真的认为要求用户首先指定一个目录将是最好的解决方案。

0

考虑使用Environment.SpecialFolder.MyMusicEnvironment.SpecialFolder.MyDocuments作为路径的一部分,这应该至少是在大多数Windows PC上工作

相关问题