怎么找到文件的扩展名

时间:2016-03-31 07:02:31

标签: c#

string[] sAllowedExt = new string[] { 
  ".jpg", ".jpeg", ".gif", ".png",".pdf", ".docx", ".doc" };

(!sAllowedExt.Contains(file.FileName.Substring(file.FileName.IndexOf('.')).ToLower()))

无效。请提出别的建议。

2 个答案:

答案 0 :(得分:5)

我会使用Path.GetExtension来确定您的文件扩展名是否有效。

string[] sAllowedExt = new string[] { 
   ".jpg", ".jpeg", ".gif", ".png", ".pdf", ".docx", ".doc" };

string Extension = System.IO.Path.GetExtension(file.FileName);

bool Result = sAllowedExt
  .Any(x => x.Equals(Extension, StringComparison.CurrentCultureIgnoreCase));

答案 1 :(得分:-2)

你找到了最后一个索引,所以你必须找到lastof的索引。

file.FileName.Substring(file.FileName.LastIndexOf('.')).ToLower());
相关问题