简体   繁体   English

不带通配符的 DirectoryInfo GetFiles

[英]DirectoryInfo GetFiles without wildcard

DirectoryInfo source = new DirectoryInfo(src);
DirectoryInfo destination = new DirectoryInfo(dest);

FileInfo[] files = source.GetFiles("myfile.mp3);

is using DirectoryInfo GetFiles without "*" in the parameter ok?使用参数中没有"*"DirectoryInfo GetFiles好吗? This would just get 1 file everytime?这每次只会得到1个文件?

Your code will only retrieve all files with the exact name that you provide.您的代码将仅检索具有您提供的确切名称的所有文件。 So if you use a wildcard like this:因此,如果您使用这样的通配符:

FileInfo[] files = source.GetFiles("myfile.*");

then all files named "myfile" of any file type will now be in your file array.那么任何文件类型的所有名为“myfile”的文件现在都将在您的文件数组中。

You can test that this is using the single file you enumerated without a wildcard by running the code here (replacing directories as needed):您可以通过在此处运行代码(根据需要替换目录)来测试这是否使用了您在没有通配符的情况下枚举的单个文件:

DirectoryInfo source = new DirectoryInfo(@"C:\Users\admin\Documents");

FileInfo[] files = source.GetFiles("myfile.mp3");
foreach (FileInfo file in files)
{
    Console.WriteLine(file.Length);
}

This will write a single line to the console if "myfile.mp3" exists in the Documents directory of the Admin account, or it will do nothing if the file does not exist because the foreach section will act upon nothing.如果“myfile.mp3”存在于 Admin 帐户的 Documents 目录中,这将向控制台写入一行,或者如果该文件不存在,它将不执行任何操作,因为foreach部分将不执行任何操作。

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

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