2016-03-31 28 views
-1
string[] sAllowedExt = new string[] { 
    ".jpg", ".jpeg", ".gif", ".png",".pdf", ".docx", ".doc" }; 

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

不起作用。請提出其他建議。如何找到文件的擴展名

+3

'Path.GetExtension(path)'' –

回答

-2

你正在尋找最後的索引,所以你必須找到最後的索引。

file.FileName.Substring(file.FileName.LastIndexOf('.')).ToLower()); 
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));