/// <summary>
        /// 根据文件名返回文件类型
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns>返回文件的类型</returns>
        public string TypeOfFile(string filename)
        {            if (filename.EndsWith(".txt"))
                return "文本文件";
            if (filename.EndsWith(".bat"))
                return "批处理文件";
            if (filename.EndsWith(".jpg"))
                return "图像文件";
            if (filename.EndsWith(".avi"))
                return "视频文件";
            if (filename.EndsWith(".exe"))
                return "可执行文件";
            if (filename.EndsWith(".rar") || filename.EndsWith(".zip"))
                return "压缩文件";
            return "未识别文件类型";
        }//string TypeOfFile(string filename)中间那几个if只是一小部分的话,还有很多if
我想知道如果这段代码的话怎么改的更简练下,我确定有更简单的办法的不知道用那个枚举或者什么的成不成

解决方案 »

  1.   

                string str = filename.Substring(filename.Length - 4);
                switch (str)
                {
                    case ".txt":
                        return "文本文件";
                    case ".rar":
                    case ".zip":
                        return "批处理文件";
                    default:
                        return "未识别文件类型";
                }
      

  2.   

            private string[,] fileTypes = new string[,]{
                {".txt","文本文件"},
                {".rar","压缩文件"},
                {".zip","压缩文件"}
            };
            public string TypeOfFile(string filename)
            {
                int tcount=fileTypes.GetLength(0);            for (int ii = 0; ii < tcount; ii++)
                {
                    if (filename.EndsWith(fileTypes[ii, 0])) return fileTypes[ii,1];
                }
                return "未识别文件类型";
            }
      

  3.   

    这样可以
    不过好像在c#里面switch 是不能穿越的,所以在 rar 后面也加个return"批处理文件";
      

  4.   

    filename.Length-4不行,并不是所有的后缀名都是.xxx的有可能出现.xxxx的情况
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace WindowsApplication161
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            MessageBox.Show(TypeOfFile(@"c:\123.exe"));
            }        public string TypeOfFile(string filename)
            {
                String Filters = ".txt|文本文件|.bat|批处理文件|.jpg|图像文件|"
                    + ".avi|视频文件|.exe|可执行文件|.rar|压缩文件|.zip|压缩文件";
                String Result = "未识别文件类型";
                String Ext = Path.GetExtension(filename).ToLower();
                String[] S = Filters.Split(new Char[] { '|' });            for (int i = 0; i < S.Length; i++)
                    if (S[i] == Ext)
                        return S[i + 1];            return Result;
            }
        }
    }
      

  6.   

     string str = Path.GetExtension(filename);
                switch (str)
                {
                    case ".txt":
                        return "文本文件";
                    case ".rar":
                    case ".zip":
                        return "批处理文件";
                    default:
                        return "未识别文件类型";
                }
      

  7.   

      <appSettings>
        <add key="UploadImagePath" value="/upload/images/"/>
        <add key="AllowUploadImageExtension" value=".jpg,.png,.jpeg,.gif,.bmp"/>
      </appSettings>                string[] allowUploadImg = ConfigSettings.GetAllowUploadImageExtension();
                    foreach (string s in allowUploadImg)
                    {
                        if (s == fileExtension)
                        {
                            allow = true;
                            break;
                        }
                    }
                    if (!allow)
                    {
                        ur.Result = false;
                        ur.Message = "上传的图片格式不正确";
                        return ur;
                    }        //允许上传的图片格式
            public static string[] GetAllowUploadImageExtension()
            {
                string str = System.Configuration.ConfigurationManager.AppSettings["AllowUploadImageExtension"].ToString();
                return str.Split(new char[] { ',' });
            }
      

  8.   

    用数组循环遍历。或者switch!!!
      

  9.   


    SortedList<string, string> FileList = new SortedList<string, string>();
                    FileList.Add(".txt", "文本文件");
                    ....
                    FileList.Add(".rar", "压缩文件");                string filename = "";
                    string TypeOfFile = FileList[filename];
      

  10.   

    或者你配置为xml文件。直接读取就行了
      

  11.   

    去维护一个hashTable就成了。如果文件类型不算很多的话,直接使用Enum枚举也成
      

  12.   

    6楼在较少量的情况下最好用,多的话用哈希表,再多的话用就用数据库喽,或者用XML
      

  13.   

                Type oType = System.Type.GetTypeFromProgID("Scripting.FileSystemObject");
                object oFSO = System.Activator.CreateInstance(oType);
                object oFile = oType.InvokeMember("GetFile",System.Reflection.BindingFlags.InvokeMethod,null,oFSO,new Object[]{@"d:\a.xml"});
                object s = oFile.GetType().InvokeMember("Type",System.Reflection.BindingFlags.GetProperty,null,oFile,null);
                MessageBox.Show(s.ToString);
    麻辣隔壁的,不会C#语法,写了半天写不出来。
    LZ你看着改吧,这个应该是比较方便的.
    改好了俺也学学。
      

  14.   

    switch (filename.Substring(filename.LastIndexOf(".")))http://www.mybuffet.cn
      

  15.   

            private Dictionary<string, string> fileTypeDic = new Dictionary<string, string>();        protected void InitFileTypeDic()
            {
                //此处可调用xml,inc,数据库或其他conf操作
                fileTypeDic.Add(".exe", "可执行程序");
                fileTypeDic.Add(".txt", "文本文件");
            }        protected string GetFileTypeName(string endWith)
            {
                if (fileTypeDic.ContainsKey(endWith.ToLower()))
                    return fileTypeDic[endWith];
                return "未识别文件类型";
            }
      

  16.   

    switch就可以了,100多个,不多了,应该不用考虑效率了