如何从不同目录中的不同文件获取路径? [英] How to get path from different files in different directories?

查看:151
本文介绍了如何从不同目录中的不同文件获取路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取目录中每个文件的路径

I need to get path for each file in directories


例如:c:/a/b/c/1.jar和c:/dir/bin/2.jar必须保存为字符串

eg: c:/a/b/c/1.jar and c:/dir/bin/2.jar must be saved as string

c:/a/b/c/1.jar; c:/ dir / bin /2.jar; ...

"c:/a/b/c/1.jar; c:/dir/bin/2.jar;..."

但是文件夹的名称可能会在将来更改,我不想写这个手动

But the folders name may change in the future and I don't want to write this manually

感谢您的帮助

编辑1:
我'我得到了几个文件夹的文件夹。在每个文件夹中都是文件。我需要在一个字符串中获取所有文件目录。例如:dir1; dir2; dir3; ...
但是我可以只给出主文件夹c:/ bin的目录

EDIT 1: I've got the folder with few folders into. in each folder is files. I need to get all files directory in one string. eg: "dir1; dir2; dir3; ..." but I can give only directory of main folder "c:/bin"

编辑2:由Sayse解决

推荐答案

您可以使用 Directory.EnumerateFiles

var allFiles = Directory.EnumerateFiles(sourceDirectory,
          "*.*", //also can use "*.jar" here for just jar files
            SearchOption.AllDirectories);

如果您希望所有文件都在一个长字符串中,那么您可以使用

If you wish for all files to be in one long string then you can use

var fileString = string.Join(",", allFiles);

如果您想要的唯一目录

var allDirs = Directory.EnumerateDirectories("...",
    "*",
     SearchOption.AllDirectories);
var dirString = string.Join(";", allDirs);

这篇关于如何从不同目录中的不同文件获取路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆