返回具有特定扩展名的文件的枚举的最佳方式



我需要根据电子表格的文件扩展名创建电子表格的枚举,我需要递归和非递归两个选项,并且我需要从枚举中返回枚举。我有这个代码

using System;
using System.IO;
using System.IO.Enumeration;
using System.Collections.Generic;
using Enumerate_org = System.Collections.Generic.IEnumerable<string>;
public IEnumerable<T> Enumerate_Original<T>(string argument1, string argument3)
{
Enumerate_org<T> org_enumeration = new Enumerate_org<T>();
// Recurse enumeration of original spreadsheets from input directory
if (argument3 == "Recurse=Yes")
{
org_enumeration = Directory.EnumerateFiles(argument1, "*.*", SearchOption.AllDirectories)
.Where(file => file.EndsWith(".fods") || file.EndsWith(".ods") || file.EndsWith(".ots") || file.EndsWith("xla") || file.EndsWith(".xls") || file.EndsWith(".xls") || file.EndsWith(".xlt") || file.EndsWith(".xlam") || file.EndsWith(".xlsb") || file.EndsWith(".xlsm") || file.EndsWith(".xlsx") || file.EndsWith(".xltm") || file.EndsWith(".xltx"))
.ToList();
return org_enumeration;
}
// No recurse enumeration
else
{
org_enumeration = Directory.EnumerateFiles(argument1, "*.*", SearchOption.TopDirectoryOnly)
.Where(file => file.EndsWith(".fods") || file.EndsWith(".ods") || file.EndsWith(".ots") || file.EndsWith("xla") || file.EndsWith(".xls") || file.EndsWith(".xls") || file.EndsWith(".xlt") || file.EndsWith(".xlam") || file.EndsWith(".xlsb") || file.EndsWith(".xlsm") || file.EndsWith(".xlsx") || file.EndsWith(".xltm") || file.EndsWith(".xltx"))
.ToList();
return org_enumeration;
}
}

但是我得到错误

The using alias 'Enumerate_org' cannot be used with type arguments
  1. 我做错了什么?

  2. 我有一系列可以接受的文件扩展名。如何更换

file.EndsWith("a specific extension")

使用相同的搜索,但试图匹配扩展数组中的任何条目?

错误是因为您在using Enumerate_org = System.Collections.Generic.IEnumerable<string>;中为已关闭(特定的、已解析的(类型<string>声明了一个类型别名,然后尝试使用它new Enumerate_org<T>();

这在两个方面是错误的——你不能重新键入或";untype";(返回到<T>并且不能实例化接口IEnumerable.

您本可以使用别名并说Enumerate_org org_enumeration = new List<string>();,但这会使类型别名变得多余(所以去掉它,只说using System.Collections.Generic;(。

然后。。。

var org_enumeration = new List<string>(); // question #1, instead of new System.Collections.Generic.List<string>();
:
... Where(file => ExtensionsList.Contains(Path.GetExtension(file)))... // question #2

在返回之前,请返回IEnumerable<string>,并且不要对可枚举对象调用.ToList()。以下是一些阅读:C#:IEnumerable、yield return和惰性评估

我建议使用Path类来获得扩展:

// Since we enumerate files' names, we return IEnumerable<string>
public static IEnumerable<string> Enumerate_Original(string argument1,
string argument3) {
var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
//TODO: add all required extensions here
".fods",
".ods",
};
return Directory
.EnumerateFiles(argument1, 
"*.*", 
argument3 == "Recurse=Yes" 
? SearchOption.AllDirectories 
: SearchOption.TopDirectoryOnly)
.Where(file => extensions.Contains(Path.GetExtension(file)));
}

AlanK让我走上了正轨,我把IEnumerable变成了一个列表。现在的代码是:

public List<string> Enumerate_Original(string argument1, string argument3)
{    
var org_enumeration = new List<string>();
// Recurse enumeration of original spreadsheets from input directory
if (argument3 == "Recurse=Yes")
{
org_enumeration = (List<string>)Directory.EnumerateFiles(argument1, "*.*", SearchOption.AllDirectories)
.Where(file => file_format.Contains(Path.GetExtension(file)))
.ToList();
return org_enumeration;
}
// No recurse enumeration
else
{
org_enumeration = (List<string>)Directory.EnumerateFiles(argument1, "*.*", SearchOption.TopDirectoryOnly)
.Where(file => file_format.Contains(Path.GetExtension(file)))
.ToList();
return org_enumeration;
}
}

最新更新