First of all sorry for my bad english but im italian.
Second, i only program from 4 months and im pretty bad...
So, I were doing a music player with Windows Forms, you can open a folder with music files and listen to them so when it open the folder, it takes all the files and put it in the listbox but they are like "C:\Desktop\Folder\AllStar.mp3" and i only wanted it like "AllStar.mp3" so i made this code but when I run it, it creates a thing, in italian its (Raccolta) [with google translator is (Collection) or (Gathering)] and it doesn't gives me the Replaced files with the short name, how can I fix that?
This is the code
string text = listBox1.GetItemText(listBox1.Items);
text = text.Replace(@"C:\Users\****\****\Desktop\-PC-\Musica", "");
listBox1.Items.Add(text);
It will help a lot!
You can use Path.GetFileName
to only retrieve the filename without the extension.
Full Example
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace ListBoxExample
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
using (var browser = new FolderBrowserDialog())
{
DialogResult result = browser.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(browser.SelectedPath))
{
var files = Directory
.GetFiles(browser.SelectedPath)
.Where(path => Path.GetExtension(path).ToUpper().EndsWith("MP3"))
.Select(path => Path.GetFileName(path));
foreach (var file in files)
{
MusicListBox.Items.Add(file);
}
}
}
}
}
}