2017-09-29 19 views
-4

當我在我的計算機中選擇多個文件並打開時,每個文件在我的應用程序中打開一個實例。在配置Windows應用程序運行一個實例,但一個文件與我的應用程序打開。如何打開選定的文件,並在我的應用程序列表框中添加地址文件。如何關聯c中的多個文件#

的Program.cs

static class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     if (args != null && args.Length > 0) 
     { 
      string fileName = args[0]; 
      if (File.Exists(fileName)) 
      { 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 

       Form1 MainFrom = new Form1(); 
       MainFrom.OpenFile(fileName); 
       Application.Run(MainFrom); 
      } 
      else 
      { 
       MessageBox.Show("The file does not exist!", "BMPlayer Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 
       Application.Run(new Form1()); 
      } 
     } 
     else 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 

Form.cs

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

這個代碼只對一個文件進行操作。

+0

你嘗試過什麼嗎?你有任何代碼與示例數據分享?我不清楚你想達到什麼目的。 – Pac0

+4

另外,你不應該給你的句子的每個單詞放一個大寫字母。 – Pac0

+0

我知道你不是英語母語的人。讓我試圖強化這個問題,並告訴它是否碰到了問題:在資源管理器中,您要選擇多個文件。現在,如果您按Enter鍵,則希望您的應用程序打開時顯示所選文件的列表,而不是每個選定文件的應用程序的一個實例。您嘗試配置您的應用程序時,一次只能打開一個實例,導致只有一個實例包含選擇中的一個文件。那是對的嗎? – Fildor

回答

0

我不知道什麼是你的桌面項目的類型,但是你可以使用下面的方法做它的WinForms:

private void OpenFiles() 
    {    
     using (var openFolderDialog = new OpenFileDialog { Multiselect = true }) 
     { 
      openFolderDialog.Filter = "csv files (*.csv)|*.csv|txt files (*.txt)|*.txt|All files (*.*)|*.*"; 

      if (openFolderDialog.ShowDialog() == DialogResult.OK 
       && openFolderDialog.FileNames.All(x => !string.IsNullOrEmpty(x))) 
      { 
       string[] files = openFolderDialog.FileNames; 

       // TO DO: You can now loop over the selected files 
       foreach(var fileName in files) 
       { 
        // Addyour logic here 
       } 
      } 
     } 
    } 

使用Windows Media Player播放器,以歌曲的列表程序示例從像一個播放列表一個WMP單個進程文件:

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     string space = " "; 
     string filePath1 = @"D:\mysong1.mp3"; 
     string filePath2 = @"D:\mysong2.mp3";    

     List<string> songFiles = new List<string>(); 
     songFiles.Add(filePath1); 
     songFiles.Add(filePath2);    

     var playList = songFiles.Aggregate((a, b) => a + space + b); 

     Process.Start("wmplayer.exe", playList);    
    } 
} 

您可以使用上述兩個碼的組合來完成你的需要。

+0

我不是說打開文件對話框項目。我的程序是一個音樂播放器,當你雙擊一個音樂時,它會播放很好,但是當你選擇多首歌曲時,程序打開歌曲在更多情況下 – Mohsen

+0

我不想在一個實例中選擇和播放多首歌曲,就像播放列表一樣。除此之外,我知道如何在C#中創建播放列表:) 所以我的問題不是「如何創建播放列表」 – Mohsen

+0

我明白你的意思,但如果你需要更多的幫助,你應該添加一些代碼來指出問題出在哪裏。否則,我可以簡單地告訴你使用上面的代碼,並循環播放選定的音樂文件並實現foreach文件邏輯來播放它,並在播放完成時關閉它。 –