System.IO.Path.GetExtension(filePath)
或者
var dot = filePath.LastIndexOf(".");
return dot == -1 ? string.Empty :filePath.Substring(dot)

解决方案 »

  1.   

    用LastIndexOf(".")就可以得到最后一个点的位置,然后substring
      

  2.   

    System.IO.Path.GetExtension(filePath)
    这个才是最简单,也是最靠谱的做法
    本身你的字符串就是个文件名,你要获取扩展名,当然是用系统自带的获取扩展名的函数获取
      

  3.   

    谢谢,我用的是System.IO.FileInfo file = new System.IO.FileInfo(txtExcelPath.Text.Trim());不知道老用文件类会不会影响性能?
      

  4.   


    会,而且FileInfo还用到了非托管资源,必须手动释放
      

  5.   


    会,而且FileInfo还用到了非托管资源,必须手动释放额,请问如何一个方法是否是非托管资源?谢谢
      

  6.   

    呵呵,正则表达式:Regex rgx=new Regex(@"(?<=\.)(\w+?)$");
      

  7.   

    理论上是这样,如果你一行一行处理,那么filePath.LastIndexOf("."); 效率高
    如果能整个文本一起处理,那么肯定用正则表达式效率高。
      

  8.   

    截取字符串示例
     class Program
        {
            static void Main(string[] args)
            {
                //定义一个字符串,用来存储文件全路径
                string strAllPath = "D:\\windoslasft\\meimeixiang.exe";
                string strPath = strAllPath.Substring(0, strAllPath.LastIndexOf("\\") + 1);//获取文件路径
                string strName = strAllPath.Substring(strAllPath.LastIndexOf("\\") + 1);//获取文件名
                Console.WriteLine("文件路径:" + strPath);//显示文件路径
                Console.WriteLine("文件名:" + strName);//显示文件名
                Console.ReadLine();
            }
        }