2017-02-17 23 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上工作

相關問題