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

本文是小编为大家收集整理的关于如何从不同目录中的不同文件中获取路径?的处理方法,想解了如何从不同目录中的不同文件中获取路径?的问题怎么解决?如何从不同目录中的不同文件中获取路径?问题的解决办法?如何从不同目录中的不同文件中获取路径?问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

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

eg:c:/a/b/c/c/1.jar and c:/dir/bin/2.jar必须保存为字符串

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

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

感谢您的帮助

编辑1: 我的文件夹中有几个文件夹.在每个文件夹中是文件.我需要在一个字符串中获取所有文件目录.例如:" dir1; dir2; dir3; ..." 但是我只能提供主文件夹的目录" c:/bin"

编辑2:通过Sayse

解决

推荐答案

您可以使用 directory.enumeratefiles

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

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

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

如果您只需要目录

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

其他推荐答案

class Program
{

    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo("C:\\");
        FullDirList(di, "*");
        Console.WriteLine("Done");
        Console.Read();
    }
    static string myfolders = "";// Your string, which inclueds the folders like this: "c:/a/b/c; c:/dir/bin;..."
    static string myfiles = ""; // Your string, which inclueds the file like this: "c:/a/b/c/1.jar; c:/dir/bin/2.jar;..."
    static List<FileInfo> files = new List<FileInfo>();  // List that will hold the files and subfiles in path
    static List<DirectoryInfo> folders = new List<DirectoryInfo>(); // List that hold direcotries that cannot be accessed
    static void FullDirList(DirectoryInfo dir, string searchPattern)
    {

        // Console.WriteLine("Directory {0}", dir.FullName);
        // list the files
        try
        {
            foreach (FileInfo f in dir.GetFiles(searchPattern))
            {
                //Console.WriteLine("File {0}", f.FullName);
                files.Add(f);
                myfiles += f.FullName + ";";
            }
        }
        catch
        {
            Console.WriteLine("Directory {0}  \n could not be accessed!!!!", dir.FullName);
            return;  // We alredy got an error trying to access dir so dont try to access it again
        }

        // process each directory
        // If I have been able to see the files in the directory I should also be able 
        // to look at its directories so I dont think I should place this in a try catch block
        foreach (DirectoryInfo d in dir.GetDirectories())
        {
            myfolders += d.FullName + ";";
            folders.Add(d);
            FullDirList(d, searchPattern);
        }
    }
}

myfiles包含所有文件,例如" c:\ myProgram1.exe; c:\ myFolder \ myprogram2.2.exe; c:\ myfolder2 \ myProgram2.dll"

myfolder包含所有文件夹,例如" C:\ myFolder; c:\ myFolder2";

本文地址:https://www.itbaoku.cn/post/634125.html

问题描述

I need to get path for each file in directories

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;..."

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

Thanks for help

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"

EDIT 2: Solved by Sayse

推荐答案

You can use 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);

If its only directories you want

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

其他推荐答案

class Program
{

    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo("C:\\");
        FullDirList(di, "*");
        Console.WriteLine("Done");
        Console.Read();
    }
    static string myfolders = "";// Your string, which inclueds the folders like this: "c:/a/b/c; c:/dir/bin;..."
    static string myfiles = ""; // Your string, which inclueds the file like this: "c:/a/b/c/1.jar; c:/dir/bin/2.jar;..."
    static List<FileInfo> files = new List<FileInfo>();  // List that will hold the files and subfiles in path
    static List<DirectoryInfo> folders = new List<DirectoryInfo>(); // List that hold direcotries that cannot be accessed
    static void FullDirList(DirectoryInfo dir, string searchPattern)
    {

        // Console.WriteLine("Directory {0}", dir.FullName);
        // list the files
        try
        {
            foreach (FileInfo f in dir.GetFiles(searchPattern))
            {
                //Console.WriteLine("File {0}", f.FullName);
                files.Add(f);
                myfiles += f.FullName + ";";
            }
        }
        catch
        {
            Console.WriteLine("Directory {0}  \n could not be accessed!!!!", dir.FullName);
            return;  // We alredy got an error trying to access dir so dont try to access it again
        }

        // process each directory
        // If I have been able to see the files in the directory I should also be able 
        // to look at its directories so I dont think I should place this in a try catch block
        foreach (DirectoryInfo d in dir.GetDirectories())
        {
            myfolders += d.FullName + ";";
            folders.Add(d);
            FullDirList(d, searchPattern);
        }
    }
}

myfiles includes all files , like "C:\MyProgram1.exe;C:\MyFolder\MyProgram2.exe;C:\MyFolder2\MyProgram2.dll"

myfolder inclueds all folders, like "C:\MyFolder;C:\MyFolder2";

查看更多