简体   繁体   English

如何在不包含目录路径的情况下返回目录文件/文件夹?

[英]How can I return a directories files/folders without the directory path included?

I am trying to extract a list of files within a folder and am currently using: 我正在尝试提取文件夹中的文件列表,目前正在使用:

 string[] files = Directory.GetFiles(txtbxNewFolder.Text);

But that returns things like "C:\\Users\\Dahlia\\Desktop\\New Folder\\jerry.txt". 但这会返回“C:\\ Users \\ Dahlia \\ Desktop \\ New Folder \\ jerry.txt”之类的内容。 Is there a way to return only "jerry.txt", or do I need to do some sort of split on the array strings? 有没有办法只返回“jerry.txt”,还是我需要对数组字符串进行某种拆分?

I am also trying to return a list of folders within a directory and am currently using: 我还试图返回目录中的文件夹列表,目前正在使用:

string[] folders = Directory.GetDirectories(txtbxOldFolder.Text);

But that returns things like "C:\\Users\\Dahlia\\Desktop\\New Folder\\folder1". 但这会返回“C:\\ Users \\ Dahlia \\ Desktop \\ New Folder \\ folder1”之类的内容。 Is there a way to return only "folder1", or do I need to do some sort of split on the array strings? 有没有办法只返回“folder1”,还是我需要对数组字符串进行某种拆分?

Using LINQ you can get a list of just the files: 使用LINQ,您可以获得仅包含文件的列表:

Directory.GetFiles(txtbxNewFolder.Text).Select(f => Path.GetFileName(f));

Though rather than GetFiles I'd probably use: 虽然不是GetFiles我可能会使用:

Directory.EnumerateFiles(txtbxNewFolder.Text).Select(f => Path.GetFileName(f));

It isn't as simple to get the directory name, but this should work (untested): 获取目录名称并不是那么简单,但这应该有效(未经测试):

Directory.GetDirectories(txtbxOldFolder.Text)
    .Select(d => new DirectoryInfo(d).Name);

Similarly, there is a: 同样,有一个:

Directory.EnumerateDirectories(txtbxOldFolder.Text)
    .Select(d => new DirectoryInfo(d).Name);

You could use Path.GetFileName and LINQ 您可以使用Path.GetFileName和LINQ

eg: 例如:

string[] files = Directory.GetFiles(txtbxNewFolder.Text)
                          .Select(f => Path.GetFileName(s))
                          .ToArray();

Have a look at the FileInfo and DirectoryInfo classes. 看看FileInfo和DirectoryInfo类。

You can do: 你可以做:

foreach (String file in files) {
    var fi = new FileInfo(file);
    Console.Out.WriteLine(fi.Name);
}

Similar for DirectoryInfo. 与DirectoryInfo类似。

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

相关问题 如何列出具有目录级别的文件和目录 - How can I list files and directories with them directory levels 如何在C#中压缩一些没有根目录的文件和目录? - How do I Zip some files and directories without a root directory in C#? 如何获取文件夹的目录,然后获取其目录的目录,依此类推……? - How can I get directories of a folder and then its directories' directory and so on…? 如何将文件夹复制到目录? - How can I copy folders to a directory? 我如何使该方法仅返回文件列表,而不返回目录? - How can i make the method to return a List of files only and not also directories? File.OpenRead()目录,子目录,文件,然后将文件夹和文件写入另一个目录 - File.OpenRead() Directories , Sub Directories , Files, and then Write Folders and files to another directory 如何删除包含文件的文件夹? - How can I delete folders with files in it? 如何将文件夹文件加载到ListView中? - How can I load a folders files into a ListView? 如何使用里面的文件返回我的文件夹列表? - How do I return a list of my folders with the files inside? 如何在没有异常的情况下将文件从目录移动到另一个目录 - How can I move my files from my directory to another directory without getting an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM