2016-07-08 74 views
-1

我得到這個錯誤:GetExtension方法錯誤

would not find a GetExtension method

代碼:

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

你加'System.IO'到您的項目? –

+1

你的班級有沒有機會叫'Path'?請發佈[mcve]。 (這也不清楚這與ASP.NET有什麼關係。) –

+0

事實上,正如其他人所說...嘗試System.IO.Path.GetExtension –

回答

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

Important: this is a console application! not a asp.net application! (you have added the asp.net tag..)