我在文件主扩展名为“.123”的目录中遍历文件名。该文件被分割成扩展名为“.456”的跨度,并且被命名为主文件,但在结尾处具有变化的数字序列。区分类似于命名文件的跨度
我给出了主文件名“CustomerName_123456_Name-of-File.123”,并且还需要查找所有的span文件。我面临的问题是,如果有两个文件命名几乎相同,我最终会捕获所有文件。
CustomerName_123456_Name-的-File.123
CustomerName_123456_Name-的-File001.456
CustomerName_123456_Name-的-File002.456
CustomerName_123456_Name-的-File002.456
CustomerName_123456_Name-的 - 文件 - Thats-几乎Identical.123
CustomerName_123456_Name-的 - 文件 - 那 - 几乎Identical001.456
CustomerName_123456_Name-的 - 文件 - 那 - 几乎Identical002.456
CustomerName_123456_Name-的 - 文件 - 那 - 几乎订货号ical002.456
我正在使用一些非常基本和有限的代码来完成我目前的结果。
public static string[] GetFilesInDirectory(string FileName, string Path)
{
string[] FilesInPath = { "" };
List<string> results = new List<string>();
try
{
FilesInPath = Directory.GetFiles(Path);
foreach (string FileInPath in FilesInPath)
{
if (FileInPath.IndexOf(Path.GetFileNameWithoutExtension(FileName)) > -1)
{
results.Add(Path.GetFileName(FileInPath));
}
}
FilesInPath = null;
return results.ToArray();
}
catch (Exception ex)
{
return results.ToArray();
}
}
如果我调用函数GetFilesInDirectory('CustomerName_123456_Name-of-File.123', 'C:\')
它将返回所有文件。
有没有更好的更准确的方法来实现这个目标?
UPDATE:
我写了使用从回答一些建议一些逻辑:
public static string[] GetImageFilesInDirectory(string FileName, string Path)
{
string[] FilesInPath = { "" };
List<string> results = new List<string>();
try
{
FilesInPath = Directory.GetFiles(Path, Path.GetFileNameWithoutExtension(FileName) + "???.???", SearchOption.TopDirectoryOnly);
foreach (string FileInPath in FilesInPath)
{
if (Path.GetExtension(FileInPath).ToLower() == Path.GetExtension(FileName).ToLower())
{
if (Path.GetFileNameWithoutExtension(FileInPath) == Path.GetFileNameWithoutExtension(FileName))
{
results.Add(Path.GetFileName(FileInPath));
}
}
else
{
if (FileInPath.IndexOf(Path.GetFileNameWithoutExtension(FileName)) > -1)
{
results.Add(Path.GetFileName(FileInPath));
}
}
}
FilesInPath = null;
return results.ToArray();
}
catch (Exception ex)
{
return results.ToArray();
}
}
正在修改命名模式的一个选项? – 2014-12-03 18:13:23