简体   繁体   English

在C#中如何在没有完整路径的情况下获取下拉菜单中的文件名

[英]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: 在C#中,我使用它来将文件名放入下拉列表中:

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 我如何只获取没有完整路径的文件名

You can use Path.GetFileName() method on the output of Directory.GetFiles() 您可以在Directory.GetFiles()的输出上使用Path.GetFileName()方法。

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

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

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM