0

I'm trying to make a C# music player and to do so, I'm using the WMP object found in win forms, I got it to load files from an specific folder on the press of a button, however, I want it to load every media file (FLAC, mp3, wav...) in an specific folder and its sub folders.

by now the code I have to load files is as follows.

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]);
               }

           }
       }


  • 1
    Does this question help: [recursively list all the files in a directory in c#](https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c)? – JonasH Jan 13 '22 at 15:59
  • 1
    Instead of `OpenFileDialog` use a FolderDialog eg. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog – Rand Random Jan 13 '22 at 15:59

2 Answers2

3

Step 1: Use FolderBrowserDialog instead of OpenFileDialog, this helps you to select a folder instead of a file

Step 2: After selecting a file, You can use the method Directory.EnumerateFiles(Your_Path, ".", SearchOption.AllDirectories) to get all files in the selected folder.

Võ Quang Hòa
  • 2,688
  • 3
  • 27
  • 31
1

Try this:

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;
                }
            }
        }
    }
  • Thank you very much for that, it works and it displays files in the folder, however, it only displays files in the selected folder and not in child folders, how could I get to do this and also how could I just show safe file names instead of the path? – Staling Marin Jan 13 '22 at 18:04
  • I tried using the ennumerateFiles method but it asks for the filetype which was declared previously and doesn't really take it as in the example in the documentation since it says that it can't convert string to string[] – Staling Marin Jan 13 '22 at 18:34
  • So I got to fix it by adding more arguments to GetFiles, i did so like this ```string[] Files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);``` this way it will get the extension of the files that we asked for previously, thank you very much for your help. – Staling Marin Jan 13 '22 at 18:44