2017-08-03 92 views
0

我最近在c#WindowsFormsApplications中開發了一個音樂播放器。如何在音樂播放器中關聯多個文件c#

只有當您選擇程序默認時,一切都會變好,它可以完美地打開一個音樂文件,但是當您選擇5個音樂文件時。 5音樂播放器打開。

如何解決這個問題當你設置它默認打開多個文件就像在C#中的播放列表? 我沒有試過任何東西或任何代碼來做到這一點。 請幫忙!

這是我的Program.cs

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] args) 
    { 


     //with args(user open file with the program) 
     if (args != null && args.Length > 0) 
     { 

      string fileName = args[0]; 
      //Check file exists 
      if (File.Exists(fileName)) 
      { 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 
       Form1 MainFrom = new Form1(); 
       MainFrom.OpenFile(fileName); 
       Application.Run(MainFrom); 
      } 
      //The file does not exist 
      else 
      { 
       MessageBox.Show("The file does not exist!", "BMPlayer Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 
       Application.Run(new Form1()); 
      } 
     } 
     //without args 
     else 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 

,這是Form1.cs中打開文件

public void OpenFile(string filePath) 
    { 
     string file1 = File.ReadAllText(filePath); 
     axWindowsMediaPlayer1.URL = filePath; 
    } 
+0

有點COde不會受傷,你怎麼做你的選擇/負載?你的應用程序中是否有任何類型的標題列表? – MaxW

+0

由於您顯示了0行代碼,我只能假設一直困擾着你。我認爲你應該在應用程序的'Main'方法中處理多重文件名(在'Program.cs'中) – Nino

+1

這段代碼適用於一個文件 – Mohsen

回答

1

給你最好的方法是創建一個新的文件類型(播放)和接收作爲參數在你的應用程序中,那麼你可以在你的應用程序中管理這個文件,從這個播放列表中添加/刪除歌曲。 它是以JSon格式存儲內容的一種好方法。那麼您可以使用Nuget Packages輕鬆管理內容,例如:NewtonSoft。

我創建了一個簡單的代碼示例,然後您可以使用json和Music Object創建和管理播放列表。

private void btnLoad_Click(object sender, EventArgs e) 
    { 
     string line; 

     using (StreamReader reader = new StreamReader(@"c:\temp\music\playlist.mpl")) 
     { 
      line = reader.ReadLine(); 
     } 

     var jobj = JsonConvert.DeserializeObject<List<Music>>(line); 
    } 

    private void btnCreate_Click(object sender, EventArgs e) 
    { 
     var musiclist = new List<Music>(); 

     var objSongs = System.IO.Directory.GetFiles(@"C:\temp\music\"); 

     foreach (var song in objSongs) 
     { 
      musiclist.Add(new Music { Name = song }); 
     } 

     var ret = Newtonsoft.Json.JsonConvert.SerializeObject(musiclist); 

     using (var sw = new StreamWriter(@"c:\temp\music\playlist.mpl")) 
     { 
      sw.Write(ret); 
      sw.Flush(); 
     } 
    } 

public class Music 
{ 
    public string Name { get; set; } 
} 
+1

你能分享代碼請! – Mohsen

+0

當然,堅持下去。 :) –

+0

分享,請看看。 –