2017-03-25 8 views
-3
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<string> file1 = new List<string>(); 
      Console.WriteLine("Enter the path to the folder"); 
      string path1 = Console.ReadLine(); 

      string text = System.IO.File.ReadAllText(path1); 
     } 
    } 
} 

ファイル(テキスト)の内容をリストに入れようとしています。私はこのウェブサイトを見ようとしましたが、何も見つかりませんでした。ファイルの内容でリストを埋めることができません

+1

テキストをリストに表示しようとしましたか?また、読みやすいように質問の形式を正しく記述してください。 – swatsonpicken

+1

あなたのタイトルの文法に問題があります。将来、あなたの質問を構成するための努力をしてください。そうしなければ、人々はあなたの質問に投票し、必要な注意を払わず、適切な回答を得られません。 – Lu4

答えて

2

各行のリストを使用する場合は、File.ReadAllLinesを使用してください。

List<string> file1 = new List<string>(); 

Console.WriteLine("Enter the path to the folder"); 
string path1 = Console.ReadLine(); 

file1.AddRange(System.IO.File.ReadAllLines(path1)); 

foreach(var line in file1) 
{ 
    Console.WriteLine(line); 
} 
+0

ありがとうございますが、今は表示できません。私がしたのはConsole.WriteLine( "{0}、file1);別の方法で行う必要がありますか? – CKap

+1

foreachループを使ってリストを反復処理することができます。 – Kalten

0

これは正常に動作するはずです。

static void Main(string[] args) 
    { 
     List<string> file1 = new List<string>(); 

     Console.Write("Enter the path to the folder:"); 
     string path1 = Console.ReadLine(); 

     file1.AddRange(System.IO.File.ReadAllLines(path1)); 

     foreach(var line in file1) 
     { 
      Console.WriteLine(line); 
     } 
     Console.ReadKey(); 
    } 
0

またはこれはラムダが好きなら、これです。

static void Main(string[] args) 
    { 
     List<string> file1 = new List<string>(); 

     Console.Write("Enter the path to the folder:"); 
     string path1 = Console.ReadLine(); 

     file1.AddRange(System.IO.File.ReadAllLines(path1)); 

     file1.ForEach(line => Console.WriteLine(line)); 
     Console.ReadKey(); 
    } 
関連する問題