MaxCoder88
MaxCoder88

Reputation: 2428

Getting filenames from a directory

string[] klasorlistesi = Directory.GetFiles(path+"//notlar//");
foreach (string eleman in klasorlistesi)
    listBox1.Items.Add(eleman);

I'm getting the datas from an array with through above code inside listBox.
BTW the file has no extension.

My question is:
I want to appear the file as only "not" instead of "C:\Users\Documents\Visual Studio 2008\not" inside the Listbox.

Also, I tried a thing like below code, but it's not working:

System.IO.Path.GetFileName(@"C:\Users\Documents\Visual Studio 2008\not");

How can I do that?

Upvotes: 0

Views: 147

Answers (3)

Dominik
Dominik

Reputation: 3372

Have you tried

string s = Path.GetFileNameWithoutExtension(@"C:\Users\Documents\Visual Studio 2008\not");

Result is not

Upvotes: 0

Christoph Fink
Christoph Fink

Reputation: 23113

You could use something like

listbox1.Items.Add(eleman.Substring(eleman.LastIndexOf('\\') + 1);

Upvotes: 0

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60744

Try to change your last line to:

listBox1.Items.Add(new FileInfo(eleman).Name);

Should do the trick.

Another option that you can do is to extract it yourself like this, but it looks a bit messier. Probably a bit faster, but in your case you should not notice.

listBox1.Items.Add(eleman.Substring(eleman.LastIndexOf('\\') + 1));

Upvotes: 2

Related Questions