Document and Video file format extension verification and image real verification help class, file format truth
Using System; using System. collections. generic; using System. linq; using System. text; using System. IO; namespace Reform. folderManage. cls {public class FileHelper {/// <summary> /// image class header encoding /// </summary> public enum ImgExtension {JPG = 255216, GIF = 7173, BMP = 1, 6677, PNG = 13780} /// <summary> // file type determination // </summary> /// <param name = "fileName"> file </param> /// <param name = "fileType"> file type </param> /// <returns> </returns> public static bool IsRealFile (string fileName, string fileType) {string fileExt = Path. getExtension (fileName ). toLower (); // get the file suffix if (fileType = "image") {if (IsAllowedExtension (fileName) return true;} if (fileType = "video ") {List <string> list = new List <string> (); list. add (". flv "); list. add (". wmv "); list. add (". avi "); list. add (". 3gp "); Path. getExtension (fileName ). toLower (); // get the file suffix return IsExistEx (list, fileExt);} if (fileType = "document ") {List <string> list = new List <string> (); list. add (". doc "); list. add (". pdf "); return IsExistEx (list, fileExt);} return false ;} /// <summary> /// determine the file suffix /// </summary> private static bool IsExistEx (List <string> list, string fileExt) {if (list. contains (fileExt) // {return true;} return false;} in the list ;} # region image processing // <summary> /// whether the image type is true // </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 }}