user3624136
user3624136

Reputation: 11

Removing the path from a GetFiles return

I am attempting to simply for this bit create a list of the songs within the MyMusic folder and display them in a listbox. The strings will also be used later for voice commands but adding those will not be a problem. My problem is that despite my attempts, I have been unable to remove the path from the displayed name.

InitializeComponent();
        string path = @"C:\Users\Toby\Music";
        string[] Songs = Directory.GetFiles(path, "*.mp3", SearchOption.TopDirectoryOnly);
        List<string> SongList = new List<string>();
        int pathlngth = path.Length;
        int i = 0;
        string fix; 
        foreach (string Asong in Songs)
        {
           fix = Asong.Remove(0,pathlngth);
           fix = Asong.Remove(Asong.Length-4);
           SongList.Add(fix);
            i = i + 1;
        }
        SongList.Add("");
        SongList.Add("There are " + i + " songs");
        SongBox.Datasource = SongList;

To me at least, this should work. However the results from my Listbox will appear as so:

And so on... Any idea what's wrong? I managed to finally remove the extension. I have tried replacing pathlngth with path.Length to no change at all.

Upvotes: 0

Views: 193

Answers (4)

Hassan
Hassan

Reputation: 5430

To get FileName from a path

string strSongName = System.IO.Path.GetFileName(FileFullPath);

To get FileNameWithoutExtension from a path

string sFileNameWithOutExtension = Path.GetFileNameWithoutExtension(FileFullPath);

Your Solution:

List<string> SongList = new List<string>();
string path = @"C:\Users\Toby\Music";
string[] Songs = Directory.GetFiles(path, "*.mp3", SearchOption.TopDirectoryOnly);

SongList.Add("");
SongList.Add("There are " + Songs.Length + " songs");

foreach (string Asong in Songs)
{
    string sFileNameWithOutExtension = Path.GetFileNameWithoutExtension(Asong);
    SongList.Add(sFileNameWithOutExtension);
}

SongBox.DataSource = SongList;

Upvotes: 1

Orel Eraki
Orel Eraki

Reputation: 12196

If you care for saving the path and just don't want to display it, than any of the previous solutions will be good for you.

If you only care of of the file names than the following will be just for you.

var resultFileNames = Songs.Select(s => Path.GetFileName(s));

This will produce a new list based on Songs but will only store their file names.

Upvotes: 0

ZombieSheep
ZombieSheep

Reputation: 29963

You are assigning the value of "fix" and then immediately overwriting it.

fix = Asong.Remove(0,pathlngth);
fix = Asong.Remove(Asong.Length-4);

Should probably be

fix = Asong.Remove(0,pathlngth);
fix = fix.Remove(Asong.Length-4);

The other option is to just use Path.GetFileName(Asong); but you'll still need to manipulate it to remove the extension.

Upvotes: 1

James
James

Reputation: 82136

There is an API that already does exactly that - Path.GetFileName

foreach (string song in Songs)
{
    SongList.Add(System.IO.Path.GetFileName(song));
}

This will give you the name + extension, if you want to omit the extension you can use Path.GetFileNameWithoutExtension instead.

Upvotes: 1

Related Questions