質問

I am using Directory.GetFiles(string,string) which finds all files of a particular filetype. 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 ?

e.g)

Pseudocode

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

will list 1 and list2 have exactly the same matching files guaranteed? 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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top