I'm attempting to read the names of all files in a specific folder, then add each file name as an item in a listbox. The issue, is when doing so my listbox is displaying the full file path.
I've tried several various things and feel as if this should be incredibly easy to remove "C:\Client\TestFolder\" & ".txt" from each item. However nothing seems to be working :( any assistance is very much appreciated!
string[] filePaths = Directory.GetFiles(@"C:\Client\TestFolder\", "*.txt", SearchOption.TopDirectoryOnly);
listBox1.Items.AddRange(filePaths);
string[] titleArray = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
titleArray[i] = listBox1.Items[i].ToString();
}
Array.Sort(titleArray);
listBox1.Items.Clear();
for (int i = 0; i < titleArray.Length; i++)
{
listBox1.Items.Add(titleArray[i].ToString());
}
For example if the file path C:\Client\TestFolder\ contained 3 .txt files:
- test1.txt
- test2.txt
- test3.txt
My listbox will display:
- C:\Client\TestFolder\test1.txt
- C:\Client\TestFolder\test2.txt
- C:\Client\TestFolder\test3.txt
Desired results:
- test1
- test2
- test3