C# 验证文件扩展名是否与已批准的列表匹配

C# 验证文件扩展名是否与已批准的列表匹配,c#,regex,linq,list,C#,Regex,Linq,List,我有一个文件列表,我想对照一个已批准的扩展名列表。 批准延期的清单可以是: 强制或可选 我需要处理两个案子 检查文件列表是否包含所有必需的扩展名 文件列表只能包含已批准列表中的扩展名 我们一直在尝试使用正则表达式,因为有些情况可以组合在一起。例如,.docx和.doc被视为相同 以下是我到目前为止得到的(伪代码) List approvedExt=new List(); //M-强制性 //O-可选 approvedExt.Add(新[…]{“pdf”,“M”}); 核准文本.添加(新[

我有一个文件列表,我想对照一个已批准的扩展名列表。 批准延期的清单可以是:

  • 强制或可选
我需要处理两个案子

  • 检查文件列表是否包含所有必需的扩展名
  • 文件列表只能包含已批准列表中的扩展名
我们一直在尝试使用正则表达式,因为有些情况可以组合在一起。例如,
.docx
.doc
被视为相同

以下是我到目前为止得到的(伪代码)

List approvedExt=new List();
//M-强制性
//O-可选
approvedExt.Add(新[…]{“pdf”,“M”});
核准文本.添加(新[](docx | doc)“,“M”})//一刀切
approvedExt.Add(新[]{”(txt)”,“O”};
//示例列表
List fileList=新列表();
文件列表。添加(“123.pdf”);
fileList.Add(“123.txt”);
添加(“123.xlsx”);
文件列表。添加(“123.pdf”);
//伪码
对于approvedExt中的每个ext(必须)
{
bool checkMandatoryExt=是否有任何文件匹配?
//我看到的示例代码
全部(f=>System.Text.RegularExpressions.Regex.IsMatch(f,pattern,System.Text.RegularExpressions.RegexOptions.IgnoreCase));
}
如果(!checkMandatoryExt)
{
//处理错误
}
对于文件列表中的每个文件
{
bool allApprovedExt=是否有匹配的模式?
}
如果(!allApprovedExt)
{
//处理错误
}
上面的示例文件列表将失败2次

  • 包含
    .xlsx
    文件(不在已批准的ext列表中)
  • 不包含
    .docx
    .doc
    文件(文件列表中不包含强制扩展名)
如果列表文件通过上述两项检查,我希望能够传递文件名列表和已批准的扩展名列表,并返回
true
/
false


谢谢

如果您需要检查扩展名列表中的扩展名包含在扩展名列表中,您可以这样做:

foreach(var file in filelist)
{
    approvedExt.Contains(file.split(".").Last();
}

下面是我将如何解决它(伪代码):


问题在哪里?已更新问题的结尾。不要使用正则表达式从文件名获取扩展名。使用No,您应该使用
Path.GetExtension
。例如,考虑路径<代码> c:\My.Frase\DATAFIL< /COD>。你的方法会说文件扩展名是
Files\datafile
。据我所知,他只有文件名列表-字符串。是的,只有文件名的字符串列表。我目前的困难是如何获取已批准的扩展列表,并按强制/可选方式将其拆分。在上面,我用一个列表来存储已批准的ext列表,但我不知道是否有更好的方法来存储和过滤列表已批准的ext列表(强制/可选)。他有一个文件名列表
fileList(){“123.pdf”,“123.txt”,“123.xlsx”“123.pdf”)
@DmitriyKovalenko正如Spacko明确指出的那样,他提供的示例也是伪代码。更改集合类型对他来说很可能不是无法解决的问题(string[]-->List),并且对代码没有影响。
foreach(var file in filelist)
{
    approvedExt.Contains(file.split(".").Last();
}
public class Condition
{
    public bool Mandatory {get;set;}
    public string[] Extensions {get;set;}
}

// ...

//NOTE: includes the . before the extension
public string[] GetExtensions(IEnumerable<string> files)
{
    return files.Select(f => Path.GetExtension(f).ToLower()??"").Distinct().ToArray();
}

public bool AllConditionsOk(string[] fileNamesToCheck, Condition[] conditions)
{
    //Extract Extension only (e.g. Path.GetExtension)
    string[] extensions = GetExtensions(fileNamesToCheck);

    //Check if any existing extension is not allowed
    foreach(string extension in extensions)
    {
        if(!conditions.Any(c => c.Extensions.Contains(extension)))
            return false;
    }

    //Check if every mandatory condition is fulfilled
    foreach(Condition condition in conditions.Where(c => c.Mandatory))
    {
         if(!condition.Extensions.Any(e => extensions.Contains(e)))
             return false;
    }
    return true;
}
return extensions.Any(extension => !conditions.Any(c => c.Extensions.Contains(extension))) &&
       conditions.Where(c => c.Mandatory)
                 .All(condition => condition.Extensions.Any(e => extensions.Contains(e)));