简体   繁体   English

目录方法中的C#GetFiles函数

[英]C# GetFiles function from Directory method

I am using Directory.GetFiles(string,string) which finds all files of a particular filetype. 我正在使用Directory.GetFiles(string,string)查找特定文件类型的所有文件。 If I have two different types of files with same names but different extensions, is it possible to be guaranteed that these files lists populated are absolutely matching ? 如果我有两种名称相同但扩展名不同的文件,是否可以保证这些文件列表绝对匹配?

eg) 例如)

Pseudocode 伪代码

  List1 -> getfiles(dir,filetype2) List2 -> getfiles(dir,filetype2) 

will list 1 and list2 have exactly the same matching files guaranteed? 列表1和list2是否具有完全相同的匹配文件? I am sure it will but wondering any circumstances can they be different? 我敢肯定,但是想知道在任何情况下它们会有所不同吗?

Correct Case 正确的情况

  List1[4] is "2esDSd.filetype1" List2[4] is "2esDSd.filetype2" 

Wrong Case 错误的情况

  List1[4] is "3esDSd.filetype1" List2[4] is "2esDSd.filetype2" 

I know I can always write another extra layer of validation or sort, as it is still possible to have incorrect input. 我知道我总是可以再编写一个额外的验证或排序层,因为仍然有可能输入错误。 But wondering is is a good practice or is it unnecessary thing to do/verify given how an internal function works. 但是,想知道这是一个好习惯还是给定内部函数的工作方式,这是否不必要/验证。

You can only "guarantee" this if the file system does indeed contain matching files. 如果文件系统确实包含匹配的文件,则只能“保证”。 And in that case it would still be best to make sure you get the filenames in alphabetical order, like so: 在这种情况下,最好还是确保按字母顺序获取文件名,如下所示:

Pseudocode 伪代码

List1 -> getfiles(dir,filetype2).OrderBy(x => x.FileName).ToList();
List2 -> getfiles(dir,filetype2).OrderBy(x => x.FileName).ToList();

However, like I said, this depends on the right files existing, and your file filter (say, "*.jpg") not matching any files that do not have twins in your directory. 但是,就像我说的,这取决于现有的正确文件,并且您的文件过滤器(例如“ * .jpg”)与您目录中没有双胞胎的任何文件都不匹配。

A more robust solution would be to just retrieve all files of the two file types you want, then search for matches between the results yourself, like so: 一个更可靠的解决方案是只检索所需两种文件类型的所有文件,然后自己搜索结果之间的匹配项,如下所示:

Pseudocode 伪代码

for each filename f1 in List1
    get matchine file name f2 in List2
    if it exists, add (f1, f2) to results

where the results is then a list of 2-tuples with matching file names. 然后,结果是一个包含匹配文件名的2元组的列表。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM