-1

So I am making a little test, and when using a listbox it says "C:/Test/Text.txt" but I want it to say Text.txt. So I currently have

private void FlatButton3_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();

    string folder = @"C:/Aatrox";
    string[] txtfiles = Directory.GetFiles(folder, "*.txt");
    string[] luafiles = Directory.GetFiles(folder, "*.lua");

    foreach (var item in folder)
    {
        ListBox1.Items.Add(Path.GetFileName(Convert.ToString(txtfiles)));
    }
}

and in the ListBox it says System.String[]

Any help?

maccettura
  • 10,514
  • 3
  • 28
  • 35
Aatroxon
  • 51
  • 1
  • 1
  • 11

5 Answers5

3

If you want both the .lua files and the .txt files (and you are using .NET 4.5 or later) you can use some LINQ to grab the files you want:

ListBox1.Items.Clear();
var files = Directory.EnumerateFiles(@"C:/Aatrox")
                .Where(file => file.ToLower().EndsWith("lua") 
                       || file.ToLower().EndsWith("txt"));
foreach(var file in files)
{
    ListBox1.Items.Add(Path.GetFileName(file));
}

It might actually be faster to use a non-LINQ approach like this:

ListBox1.Items.Clear();
foreach(var file in Directory.EnumerateFiles(@"C:/Aatrox"))
{
    string extension = Path.GetExtension(file);
    if(string.Compare(extension, ".lua", true) == 0 
       || string.Compare(extension, ".txt", true) == 0)
    {
        ListBox1.Items.Add(Path.GetFileName(file));
    }
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
2

Thanks to @maccettura I have got it.

private void FlatButton3_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();

    string folder = @"C:/Aatrox";
    string[] txtfiles = Directory.GetFiles(folder, "*.txt");
    string[] luafiles = Directory.GetFiles(folder, "*.lua");

    foreach (var item in txtfiles)
    {
        ListBox1.Items.Add(Path.GetFileName(item));
    }
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
Aatroxon
  • 51
  • 1
  • 1
  • 11
  • Do you want the txt files _and_ the lua files in the listbox? – maccettura Mar 27 '18 at 19:27
  • 2
    You're not a bad coder... Juste take the time to understand the piece of code you have in front of you ;) – C1rdec Mar 27 '18 at 19:27
  • That will not display your lua files – ForeverZer0 Mar 27 '18 at 19:28
  • I have a new issue related to this, as soon as I got this code, the SelectInderChanged stopped working. `private void ListBox1_SelectedIndexChanged_1(object sender, EventArgs e) { var selectedFile = ListBox1.SelectedItem; textEditorControl1.Text = ""; textEditorControl1.Text = File.ReadAllText(Convert.ToString(selectedFile)); }` – Aatroxon Mar 27 '18 at 19:41
  • @Aatroxon you only put the file name in the listbox, not the entire path. How cal you read all text from just a filename? – maccettura Mar 27 '18 at 19:44
  • @maccettura File.ReadAllText(Convert.ToString(selFile)); – Aatroxon Mar 27 '18 at 19:46
  • @Aatroxon look at the code that adds files to the ListBox, you add `Path.GetFileName(item)`. This is adding the _file name only_ to the ListBox. All of your path information gets stripped away. If you want to read a text file you need to pass it a proper path (and not just a file name) – maccettura Mar 27 '18 at 19:48
  • sorry, but how would I do that? @maccettura – Aatroxon Mar 27 '18 at 19:49
  • @Aatroxon you either need to use a Dictionary datasource for your ListBox (with the key being the file name and the value being the path) See [this answer](https://stackoverflow.com/a/8634075/2457029) for an idea of what I mean. Or you need to rebuild the path in your IndexChange function (using `Path.Combine()`). – maccettura Mar 27 '18 at 19:53
  • Please teach me how to do this. I have googled it now with no success – Aatroxon Mar 27 '18 at 20:02
  • @Aatroxon I linked you to all you need, what about that link is giving you trouble? – maccettura Mar 27 '18 at 20:11
  • @maccettura The blue coloring didnt load and I didnt see it. Sorry – Aatroxon Mar 27 '18 at 20:14
  • @Aatroxon its ok, the link styling is very subtle. Basically what you need to do is create a dictionary where the key is the file name, and the value is the path. That way you can access the key _and_ value later by casting the selected item to `DictionaryEntry` – maccettura Mar 27 '18 at 20:16
1

You are looping in folder variable and using txtfiles variable instead of item. Also, converting a String to String is useless. Maybe your code shall be like:

private void FlatButton3_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();

    string folder = @"C:/Aatrox";
    string[] txtfiles = Directory.GetFiles(folder, "*.txt");
    string[] luafiles = Directory.GetFiles(folder, "*.lua");

    foreach (var item in txtfiles)
    {
        ListBox1.Items.Add(Path.GetFileName(item));
    }
}

Extra: if you also want to list *.lua files, you have to do another foreach loop.

Fabian_57
  • 11
  • 2
0

This is far from the most efficient answer, but one that will hopefully help you understand.

    var filenames = Directory.GetFiles("C:/Aatrox", "*.txt").ToList();
    filenames.AddRange(Directory.GetFiles("C:/Aatrox", "*.lua"));
    foreach (var filename in filenames)
        listBox.Add(Path.GetFileName(filename));
ForeverZer0
  • 2,379
  • 1
  • 24
  • 32
  • What makes you think OP wants both file types in the listbox? – maccettura Mar 27 '18 at 19:27
  • It is an assumption with high probablility, based on the original code he listed based on what he posted, but could possibly be incorrect. I see no other reason a local variable would have been created with them, and never used, within the same block that he is adding files to a listbox. – ForeverZer0 Mar 27 '18 at 19:31
0
var files = Directory.GetFiles(path, "*.*", SearchOption.Recursive);
var filter = files.Where(file => file.Contains(".txt") || file.Contains(".lua"));

foreach(var file in filter)
     listbox.Items.Add(new FileInfo(file).Name);

Code is pretty simple, grab the files within directory, use Linq to filter out the extensions you want. Then loop through, convert to FileInfo to use the Name property that uses the short name.

Greg
  • 11,302
  • 2
  • 48
  • 79