using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Reform.FolderManage.Cls { public clas... ...
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Reform.FolderManage.Cls { public class FileHelper { /// <summary> /// 圖片類型頭部編碼 /// </summary> public enum ImgExtension { JPG = 255216, GIF = 7173, BMP = 6677, PNG = 13780 } /// <summary> /// 文件類型的判斷 /// </summary> /// <param name="fileName">文件</param> /// <param name="fileType">文件類型</param> /// <returns></returns> public static bool IsRealFile(string fileName, string fileType) { string fileExt = Path.GetExtension(fileName).ToLower();//取得文件尾碼名 if (fileType == "圖片") { if (IsAllowedExtension(fileName)) return true; } if (fileType == "視頻") { List<string> list = new List<string>(); list.Add(".flv"); list.Add(".wmv"); list.Add(".avi"); list.Add(".3gp"); Path.GetExtension(fileName).ToLower();//取得文件尾碼名 return IsExistEx(list, fileExt); } if (fileType == "文檔") { List<string> list = new List<string>(); list.Add(".doc"); list.Add(".pdf"); return IsExistEx(list, fileExt); } return false; } /// <summary> /// 判斷文件尾碼 /// </summary> private static bool IsExistEx(List<string> list, string fileExt) { if (list.Contains(fileExt))//在列表中 { return true; } return false; } #region 圖片處理 /// <summary> /// 是否是真實的圖片類型 /// </summary> public static bool IsAllowedExtension(string imgFileName) { System.IO.FileStream fs = new System.IO.FileStream(imgFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); string fileclass = ""; byte buffer; try { buffer = br.ReadByte(); fileclass = buffer.ToString(); buffer = br.ReadByte(); fileclass += buffer.ToString(); } catch { } br.Close(); fs.Close(); ImgExtension[] fileEx = { ImgExtension.BMP, ImgExtension.GIF, ImgExtension.JPG, ImgExtension.PNG }; foreach (ImgExtension fe in fileEx) { if (Int32.Parse(fileclass) == (int)fe) return true; } return false; } #endregion } }