private static string GetMimeTypeByExtension(string fileName) { string mimeType; MimeTypeHelper.TryGetMimeTypeByExtension(fileName, out mimeType); return(mimeType); }
/// <summary> /// Gets the MimeType for the specified file, based on it's extension if possible, otherwise on its content. /// </summary> /// <param name="fileName">The path to the file to investigate.</param> /// <returns>The Mime Type for the file if successfull; otherwise return 'unknown/unknown'.</returns> /// <exception cref="ArgumentNullException">thrown if the <paramref name="fileName"/> is <c>Null</c> or Empty.</exception> /// <exception cref="FileNotFoundException">thrown if the file specified by the parameter <paramref name="fileName"/> does not exist.</exception> public static string GetMimeType(string fileName) { if (fileName.IsNullEmptyOrWhiteSpace()) { throw new ArgumentNullException("fileName"); } if (!File.Exists(fileName)) { throw new FileNotFoundException(string.Format("{0} not found", fileName)); } string mimeType; if (!MimeTypeHelper.TryGetMimeTypeByExtension(fileName, out mimeType)) { MimeTypeHelper.TryGetMimeTypeByData(new System.IO.FileInfo(fileName).OpenRead().ToByteArray(), out mimeType); } return(mimeType); }