0

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:

  1. test1.txt
  2. test2.txt
  3. test3.txt

My listbox will display:

  1. C:\Client\TestFolder\test1.txt
  2. C:\Client\TestFolder\test2.txt
  3. C:\Client\TestFolder\test3.txt

Desired results:

  1. test1
  2. test2
  3. test3
  • 1
    Look up the documentation for the `System.IO.Path` class. It provides methods you might need/want to solve your problem... ;-) –  Jun 22 '19 at 18:53
  • 1
    [Path.GetFileNameWithoutExtension](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfilenamewithoutextension?view=netframework-4.8) is probably what you're looking for. – Tobias Tengler Jun 22 '19 at 18:57
  • 1
    If you want [C# file name from path](https://www.bing.com/search?q=c%23+file+name+from+path) it is covered in duplicate, if you are looking for something else - please [edit] post to clarify... as code/title of what you are trying to do is complicated but sample of what you want to achieve for some reason look simply file names. – Alexei Levenkov Jun 22 '19 at 19:01
  • Tangential side note: You don't need to make the detour of of first adding the path strings to the listbox to just take them out again and replace them anyway. Its just useless work your program is doing there. Just manipulate and sort the `filePaths` array you got from `Directory.GetFiles` directly, and then fill the listbox once from this array... –  Jun 22 '19 at 19:34

1 Answers1

1

You can use Path.GetFileNameWithoutExtension.

Example using your code:

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(Path.GetFileNameWithoutExtension(titleArray[i]));
}
Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
Cobysan
  • 99
  • 6
  • Incoming nitpick ;-) Why is there a `.ToString()` in `titleArray[i].ToString()`? –  Jun 22 '19 at 18:59
  • Yes, you can also drop off your .ToString()...not needed; I simply added the 'GetFileNameWithoutExtension' to their original code. – Cobysan Jun 22 '19 at 19:03
  • @elgonzo Cannot Implicitly convert type 'object' to 'string' – Patrick Bruinsma Jun 22 '19 at 19:19
  • @PatrickBruinsma, there are no values or references of type `object` involved there. You seem to confuse something or look at an error that is related to a completely different code line elsewhere (note that my comment was specifically about `titleArray[i].ToString()`) –  Jun 22 '19 at 19:27