列出文件夹c#中的文件 [英] List files in a folder c#
本文介绍了列出文件夹c#中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有一个包含一些文件的文件夹。文件名具有前缀(ID)。现在我想在Web表单中列出具有相同前缀的所有文件。假设我有一个ID(22),我想要检索所有前缀为22的文件并列出它们。文件上传时添加了前缀...任何人都可以帮我解决
Hi
I have a folder with some files in it. The file names have a prefix (ID). Now i want to list all files with the same prefix in a web form.. Say i have an ID (22) and i want to retrieve all files with prefix 22 and list them. The Prefix is added when the files are uploaded... Can anyone help me out
推荐答案
以下是一些例子...
http:// www .dotnetperls.com / directory-getfiles [ ^ ]
http://www.csharp -examples.net/get-files-from-directory/ [ ^ ]
http://stackoverflow.com/questions/3710617/list-recursively-all-files-and-folders-under-the-giving-path 一> [ ^ ]
问候
Sebastian
Hi,
Here are some examples...
http://www.dotnetperls.com/directory-getfiles[^]
http://www.csharp-examples.net/get-files-from-directory/[^]
http://stackoverflow.com/questions/3710617/list-recursively-all-files-and-folders-under-the-giving-path[^]
Regards
Sebastian
string searchPattern="22*";
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{
return System.IO.Directory.GetFiles(path, searchPattern, searchOption);
}
var d = new DirectoryInfo("PATH-TO-YOUR-FILES");
var files = d.GetFiles().Where(f => f.Name.StartsWith("11"));
如果需要,您可以添加更多过滤器。通过添加
You can add more filter if you want. By adding
.OrderByDescending(f=>f.CreationTime)
我将获得最新的文件。如果我不想要所有文件,我可以简单地添加
I will get newest files on top. And if I don't want all files I can simple add
.Take(10)
来获取10个最新文件,依此类推......
to get the 10 newest files and so on...
这篇关于列出文件夹c#中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文