0

Im making a mp3 player in c# and im using a autoload function and it works perfectly to load and play, but the "problem" in in the list box where the .mp3 files are displayed. it shows the file directory and file extension like this:

C:\Users\Felix\Documents\songs_here\list_1\Admiral P - Engle.mp3

and insteed of that i would like it to show:

Admiral P - Engel

is this possible and how to i do it? the file load code is:

private void PopulateListBox1(string folder)
    {
        string[] files = Directory.GetFiles(folder);
        foreach (string file in files)
            listBox1.Items.Add(file);
    }



PopulateListBox1(dir1);

Thanks in advance!!

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
MoDz Trolls
  • 37
  • 2
  • 5
  • 2
    [Path.GetFileNameWithoutExtension](https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx). But you should look into reading the mp3 tags for better results: [View/edit ID3 data for MP3 files](http://stackoverflow.com/q/68283/669576). – 001 Nov 15 '16 at 14:32
  • file.Split('\\').Last().Split('.').First() that if you always have a path and an extension – Lidaranis Nov 15 '16 at 14:32
  • 1
    [Path.GetFileNameWithoutExtension](https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx) – Han Nov 15 '16 at 14:33
  • Possible duplicate of [Remove file extension from a file name string](http://stackoverflow.com/questions/7356205/remove-file-extension-from-a-file-name-string) – Mong Zhu Nov 15 '16 at 14:34

3 Answers3

3

You can use Path.GetFileNameWithoutExtension.

Path.GetFileNameWithoutExtension Method (String)
Returns the file name of the specified path string without the extension.

For example:

Path.GetFileNameWithoutExtension("C:\Users\...\songs_here\list_1\Admiral P - Engle.mp3");

Would return:

Admiral P - Engle

Update:

I'm assuming from your comment that you want to display the file name but still have a reference to the path to the file to pass to your player.
You'll need to create your own class to hold the mp3 file name and path like this:

public class MusicFile
{
    public string Path;
    public string FileName;

    public override string ToString()
    {
        return FileName;
    }
}

private void PopulateListBox1(string folder)
{
    string[] files = Directory.GetFiles(folder);

    foreach (string file in files)
    {
        var music = new MusicFile
        {
            Path = file,
            FileName = Path.GetFileNameWithoutExtension(file)
        };

        listBox1.Items.Add(music);
    }
}

This shows how to loop through each item and get the path, but you could also use events such as SelectedIndexChanged depending on your needs.

foreach (var item in listBox1.Items)
{
    var filepath = ((MusicFile)item).Path; // Shows the full path, pass this to the player
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • yes it did load the name only in to the list box but it was unable to play it, i got a message saying "The file you are attempting to play has an extension (.) that does not match the fiel format. playing the file may result in unexpected behavior. Do you want the Player to try to play this content" – MoDz Trolls Nov 15 '16 at 15:49
  • Updated to show how to list the filenames in the listbox but still get the path to pass to the player. – Equalsk Nov 15 '16 at 16:06
  • this only plays the last song in the playlist – MoDz Trolls Nov 18 '16 at 17:48
  • That's because I assume you're looping through the files as shown, passing the current file to the player but not waiting for it to finish before the loop continues to the next file so of course only the last one plays because there are non left. You need to pass the file to the player, WAIT FOR THE CURRENT FILE. TO STOP PLAYING, then let the loop continue. I can't write this bit for you. – Equalsk Nov 18 '16 at 18:04
  • no thats not it, even if i do that it just plays through the same song over and over again – MoDz Trolls Nov 19 '16 at 14:26
  • You should post a new question and show your code. I'm sure me or someone else can help :-) – Equalsk Nov 19 '16 at 14:27
1

Using Linq one line code

private void PopulateListBox1(string folder)
{
  listBox1.DataSource =Directory.GetFiles(folder).Select(x => Path.GetFileNameWithoutExtension(x)).ToList();
}
Damith
  • 62,401
  • 13
  • 102
  • 153
0

as the file naming pattern is the same, first you might want to check file extension with a string split on dot character on every file entry, then for each file if it is mp3 extension , split an pop the last word

Franck Ngako
  • 163
  • 6