gpt4 book ai didi

c# - 过滤文件名 : getting *. abc without *.abcd, or *.abcde, 等等

转载 作者:太空狗 更新时间:2023-10-29 18:19:41 24 4
gpt4 key购买 nike

Directory.GetFiles(LocalFilePath, searchPattern);

MSDN 注释:

When using the asterisk wildcard character in a searchPattern, such as ".txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files having extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, while a search pattern of "file.txt" returns both files.

以下列表显示了 searchPattern 参数的不同长度的行为:

  • *.abc 返回扩展名为 .abc.abcd.abcde 的文件>、.abcdef 等。

  • *.abcd 仅返回扩展名为 .abcd 的文件。

  • *.abcde 仅返回扩展名为 .abcde 的文件。

  • *.abcdef 仅返回扩展名为 .abcdef 的文件。

searchPattern 参数设置为 *.abc 时,如何返回扩展名为 .abc 的文件,而不是 .abcd, .abcde 等等?

也许这个功能会起作用:

    private bool StriktMatch(string fileExtension, string searchPattern)
{
bool isStriktMatch = false;

string extension = searchPattern.Substring(searchPattern.LastIndexOf('.'));

if (String.IsNullOrEmpty(extension))
{
isStriktMatch = true;
}
else if (extension.IndexOfAny(new char[] { '*', '?' }) != -1)
{
isStriktMatch = true;
}
else if (String.Compare(fileExtension, extension, true) == 0)
{
isStriktMatch = true;
}
else
{
isStriktMatch = false;
}

return isStriktMatch;
}

测试程序:

class Program
{
static void Main(string[] args)
{
string[] fileNames = Directory.GetFiles("C:\\document", "*.abc");

ArrayList al = new ArrayList();

for (int i = 0; i < fileNames.Length; i++)
{
FileInfo file = new FileInfo(fileNames[i]);
if (StriktMatch(file.Extension, "*.abc"))
{
al.Add(fileNames[i]);
}
}

fileNames = (String[])al.ToArray(typeof(String));

foreach (string s in fileNames)
{
Console.WriteLine(s);
}

Console.Read();
}

还有其他更好的解决方案吗?

最佳答案

答案是您必须进行后期过滤。单独使用 GetFiles 不能做到这一点。这是一个将对结果进行后期处理的示例。有了这个,您可以使用或不使用 GetFiles 的搜索模式 - 无论哪种方式都可以。

List<string> fileNames = new List<string>();
// populate all filenames here with a Directory.GetFiles or whatever

string srcDir = "from"; // set this
string destDir = "to"; // set this too

// this filters the names in the list to just those that end with ".doc"
foreach (var f in fileNames.All(f => f.ToLower().EndsWith(".doc")))
{
try
{
File.Copy(Path.Combine(srcDir, f), Path.Combine(destDir, f));
}
catch { ... }
}

关于c# - 过滤文件名 : getting *. abc without *.abcd, or *.abcde, 等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/437914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com