.NET 判断文件类型

77 阅读1分钟
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ToBmp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string extension = Path.GetExtension("C:\\Users\\Administrator\\Desktop\\01.pAg");


            if (extension.Equals(".pdf") )
            {
                Console.WriteLine("这是pdf");
            }
            else if(isImg(extension))
            {
                Console.WriteLine("这是图片");
            }else
            {
                throw new Exception("You need to input a PDF or image");
            }
        }

        public static bool isImg(string extension)
        {
            extension = extension.ToLower();
            List<string> list = new List<string>() { ".jpeg",".jpg",".png",".gif",".bmp"};
            bool result = false;
            for(int i = 0;i<list.Count; i++)
            {
                if (list[i].Equals(extension)) {
                    return true;
                }
            }
            return result;
        }

    }
}