You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#实现检查文件夹是否存在子目录并获取其完整路径

获取指定文件夹子目录的完整路径

嘿,你已经写了检查dump文件夹是否有子目录的代码,现在要获取这些子目录的完整路径其实非常容易,只需要利用DirectoryInfo类的FullName属性就可以啦!

咱们来修改你的代码,当检测到存在子目录时,遍历它们并获取完整路径:

private void button2_Click_1(object sender, EventArgs e) 
{
    string SubFolderFullPath = heyman + "/dump";
    DirectoryInfo directory = new DirectoryInfo(SubFolderFullPath);
    DirectoryInfo[] subdirs = directory.GetDirectories();

    if (subdirs.Length == 0)
    {
        MessageBox.Show("Dossier dump vide...");
    }
    else
    {
        // 遍历所有子目录,获取完整路径
        foreach (DirectoryInfo subdir in subdirs)
        {
            string fullPath = subdir.FullName;
            // 这里可以根据需求使用fullPath,比如输出到控制台、显示在消息框或者存储起来
            MessageBox.Show($"子目录完整路径:{fullPath}");
        }
    }
}

关键说明:

  • subdir.FullName会直接返回该子目录的绝对完整路径,不需要自己拼接字符串,避免路径格式错误。
  • 如果只需要第一个子目录的路径,不用遍历,直接取subdirs[0].FullName就可以。
  • 小建议:如果你的路径中使用了/,在Windows环境下建议换成\\或者用Path.Combine来拼接路径,这样更规范也更适配不同平台,比如:
    string SubFolderFullPath = Path.Combine(heyman, "dump");
    

内容的提问来源于stack exchange,提问作者Jobine23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:41:48