user10596830
user10596830

Reputation:

in c# how to get filenames in dropdown without the full path

I want the list of files in a folder to be populated into my dropdown list.

in c# i use this to get filenames into dropdown:

private void CasparRefresh_Click(object sender, EventArgs e)
    {

        string[] fileArray = Directory.GetFiles(@"C:\Users\JoZee\Desktop\Energy\Caspar\Server\media\");

        foreach (string name in fileArray)
        {
            cbxV1.Items.Add(name);
        }

How to i get only the filenames without the full path

Upvotes: 0

Views: 621

Answers (2)

Renatas M.
Renatas M.

Reputation: 11820

There is another option to do same:

var dirInfo = new DirectoryInfo(@"C:\Users\JoZee\Desktop\Energy\Caspar\Server\media\");
foreach (var fileInfo in dirInfo.GetFiles())
{
    cbxV1.Items.Add(fileInfo.Name);
}

Upvotes: 2

Rahul
Rahul

Reputation: 77896

You can use Path.GetFileName() method on the output of Directory.GetFiles()

   string[] fileArray = Directory.GetFiles(@"C:\Users\JoZee\Desktop\Energy\Caspar\Server\media\");

    foreach (string name in fileArray)
    {
        cbxV1.Items.Add(Path.GetFileName(name));
    }

Upvotes: 2

Related Questions