从目录中获取文件夹名称

新人
string fullpath = @"c:\User\A\1"

private void button3_Click(object sender, EventArgs e)
 { 
   listbox.items.add();
 }

我只想在列表框中的用户下显示文件夹“A”的名称。

我试过这个

string patht = @"c:\User";
string[] files = System.IO.Directory.GetFiles(patht, "*", System.IO.SearchOption.TopDirectoryOnly);
foreach(string file in files)
{
    listBox1.Items.Add(file);
}

这没有显示任何内容

阿米特·维尔玛

如果您需要文件夹名称,请使用以下代码。它将在提供的路径下为您提供父文件夹名称。

private void button1_Click(object sender, EventArgs e)
{
    string patht = @"C:\Windows";
    string[] directories = System.IO.Directory.GetDirectories(patht);
    foreach (string d in directories)
    {
        listBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(d));
    }
}

如果您还需要子文件夹,则会有单独的代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章