2016-09-13 9 views
-4

テキストファイルがあります。私のプログラムでは次のようにします:テキストファイルを読み込んで行単位で処理する

1 - 行数とファイルパスを表示します。

各行に2ループ。

var lines = File.ReadLines(@"C:\\test.txt");     

編集(回答)

public static string[] local_file; // make a string array to load the file into it 
int i = 0; // index of lines 
    try 
    { 
     OpenFileDialog op = new OpenFileDialog // use OpenFileDialog to choose your file 
     { 
      Filter = "Combos file (*.txt)|*.txt" ;// select only text files 
     } 
     if (op.ShowDialog() == DialogResult.OK) 
     { 

      local_file= File.ReadAllLines(op.FileName);// load all the contents of the file into the array 

      string count = "lines = " + Convert.ToString(local_file.Length); // number of lines 

      string file_name = op.FileName; // show the file name including the path 
     } 
     for (i; i < local_file.Length; i++)// loop through each line 
      { 
      // do something here remember to use local_file[i] for the lines 
      } 

    }catch (Exception exception) 
    { 
     MessageBox.Show(exception.Message); 
    } 
+0

ある場合 後で、それぞれの行を分割するためのユーザforeachのは、あなたが「アリ」の実際の行(あなたのループがあるを確認し、コードの一部を表示してくださいすることができます?FirstFirstOrDefault?) – nabuchodonossor

+3

すべての行が表示されますが、最初の行( 'FirstOrDefault')だけが表示されます。なぜそれが最初の行以上に読み込まれるのでしょうか?これは非常に基本的な質問のようなもので、特定の問題ではなく言語の知識が一般的でないことを示唆しています... – Chris

+0

教授Googleは "c-sharpについてテキストファイルからテキストを読む方法"について教えてくれるでしょう –

答えて

0
public static string[] local_file; // make a string array to load the file into it 
int i = 0; // index of lines 
try 
     { 
      OpenFileDialog op = new OpenFileDialog // use OpenFileDialog to choose your file 
      { 
       Filter = "Combos file (*.txt)|*.txt" ;// select only text files 
      } 
      if (op.ShowDialog() == DialogResult.OK) 
      { 

       local_file= File.ReadAllLines(op.FileName);// load all the contents of the file into the array 

       string count = "lines = " + Convert.ToString(local_file.Length); // number of lines 

       string file_name = op.FileName; // show the file name including the path 
      } 
      for (i; i < local_file.Length; i++)// loop through each line 
       { 
       // do something here remember to use local_file[i] for the lines 
       } 

     }catch (Exception exception) 
     { 
      MessageBox.Show(exception.Message); 
     } 
+0

一般的に、答えには、コードが何を意図しているのかの説明と、なぜ他のものを導入しないで問題を解決するのかについての説明があれば、はるかに役立ちます。 –

+0

@TomArandaだからこそ、私はコメントを使ってそれを答え、他の人がコードをコピーするのを理解するのを助けるために –

+0

いいえ。それは理にかなっている。 –

0

は次のようになります。

 string[] lines = File.ReadAllLines("....."); 
     foreach (string line in lines) 
     { 
      if (line.StartsWith("....")) 
      { 
      } 
     } 
+1

いいえ'ReadLines'から' ReadAllLines'に変更する必要があります。実際、 'ReadLines'はIEnumerable を返すので、メモリ内のすべての行をロードする必要はありません。これは大きなファイルを扱うときに大きな利点です。 –

+0

@Chrisこれは本当に申し訳ありませんが、私は初心者ですが、あなたの答えとあなたの時間に感謝の最初の時間です –

+0

@Shannon Holsinger私は数時間Googleを検索してきました今すぐ –

0

あなたはLINQでそれをすべて行うことができます。

var parts = File.ReadLines(@"C:\test.txt") // No need to escape backslash here since you're using a verbatim string 
    .Select(line => line.Split(':')) 
    .FirstOrDefault(p => p.Length == 2 && p[0] == "ali"); 

if (parts != null) 
{ 
    textBox1.Text = parts[0]; 
    textBox2.Text = parts[1]; 
} 
3

メイクを名前aliを含む行をフィルタリングすることで簡単です。 lines.countが0以上

var lines = File.ReadLines(@"C:\Test.txt").Where(l => l.Contains("ali")); 
関連する問題