我正在嘗試制作一個 C# 音樂播放器,為此,我正在使用在 win 表單中找到的 WMP 物件,我可以通過按下按鈕從特定檔案夾中加載檔案,但是,我希望它能夠加載特定檔案夾及其子檔案夾中的每個媒體檔案(FLAC、mp3、wav...)。
現在我必須加載檔案的代碼如下。
string[] path, files; //Global Variables to get the path and the file name
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//This function displays the files in the path and helps selecting an index
axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
axWindowsMediaPlayer1.uiMode = "None";
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if(ofd.ShowDialog() == DialogResult.OK)
{
//Function that loads the files into the list.
files = ofd.SafeFileNames;
path = ofd.FileNames;
for (int i = 0; i < files.Length; i )
{
listBox1.Items.Add(files[i]);
}
}
}
uj5u.com熱心網友回復:
第 1 步:使用FolderBrowserDialog而不是 OpenFileDialog,這可以幫助您選擇檔案夾而不是檔案
步驟 2:選擇檔案后,您可以使用方法Directory.EnumerateFiles(Your_Path, " . ", SearchOption.AllDirectories)獲取所選檔案夾中的所有檔案。
uj5u.com熱心網友回復:
試試這個:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listBox1.Items[listBox1.SelectedIndex];
axWindowsMediaPlayer1.uiMode = "None";
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == DialogResult.OK)
{
LoadFiles(FBD.SelectedPath, new string[] { ".mp3", ".wav" }); //You can add more file extensions...
}
}
private void LoadFiles(string FolderPath, string[] FileExtensions)
{
string[] Files = System.IO.Directory.GetFiles(FolderPath);
string[] Directories = System.IO.Directory.GetDirectories(FolderPath);
for (int i = 0; i < Directories.Length; i )
{
LoadFiles(Directories[i], FileExtensions);
}
for (int i = 0; i < Files.Length; i )
{
for (int j = 0; j < FileExtensions.Length; j )
{
if (Files[i].ToLower().EndsWith(FileExtensions[j].ToLower()))
{
listBox1.Items.Add(Files[i]);
break;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411196.html
標籤:
