GetExtension方法错误

时间:2016-07-08 08:13:07

标签: c# console-application

我收到了这个错误:

  

找不到GetExtension方法

代码:

public static void Main()
{
    string[] arr = {"abc.txt", "asd.TXT","bvc.pdf","fgd.txt","hss.pdf","djhd.xml"};
    var arrp = arr.Select(file=>Path.GetExtension(file).TrimStart('.').ToLower()).GroupBy(x=>x,(ext,extcnt)=>new
    {
        Extension = ext,
        count=extcnt.Count()
    });
    foreach(var v in arrp)
    {
        Console.WriteLine("{0} files(s) with {1} Extension", v.Count,v.Extension );
        Console.ReadLine();
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

首先检查,您是否添加了使用声明? using System.IO;

第二次更改您的代码行:

Console.WriteLine("{0} files(s) with {1} Extension", v.Count, v.Extension);

为:

Console.WriteLine("{0} files(s) with {1} Extension", v.count, v.Extension);

我创建了一个新的控制台应用程序。这个例子适用于我的环境:

using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arr = { "abc.txt", "asd.TXT", "bvc.pdf", "fgd.txt", "hss.pdf", "djhd.xml" };
            var arrp = arr.Select(file => Path.GetExtension(file).TrimStart('.').ToLower()).GroupBy(x => x, (ext, extcnt) => new
            {
                Extension = ext,
                count = extcnt.Count()
            });
            foreach (var v in arrp)
            {
                Console.WriteLine("{0} files(s) with {1} Extension", v.count, v.Extension);
                Console.ReadLine();
            }   
        }
    }
}

我得到了这个结果:

enter image description here

  

重要提示:这是一个控制台应用程序!不是asp.net应用程序! (你已经添加了asp.net标签..)