2017-08-30 188 views
0

我想逐行讀取文本文件並編輯特定行。所以,我把文本文件轉換成一個字符串變量,如:C#逐行讀取文本文件並編輯特定行

string textFile = File.ReadAllText(filename);

我的文本文件是這樣的:

Line A 
Line B 
Line C 
Line abc 
Line 1 
Line 2 
Line 3 

我有一個特定的字符串(=「ABC」),這是我想要在這個文本文件中搜索。所以,我讀了行,直到找到字符串,並要到第三行(「3號線」 - >這行永遠是不同的)是找到字符串後:

string line = ""; 
string stringToSearch = "abc"; 

using (StringReader reader = new StringReader(textFile)) 
    { 
     while ((line = reader.ReadLine()) != null) 
     { 
      if (line.Contains(stringToSearch)) 
       { 
       line = reader.ReadLine(); 
       line = reader.ReadLine(); 
       line = reader.ReadLine(); 

       //line should be cleared and put another string to this line. 
       } 
     } 
    } 

我要清除的第三讀出線並將另一個字符串放在這一行,並將整個「字符串」保存到「textFile」中。

我該怎麼做?

+0

這可能有助於:[File.ReadAllLines](https://msdn.micro rosoft.com/en-us/library/system.io.file.readalllines(v=vs.110).aspx)。使用它,遍歷所有行,替換你想替換的行,然後回寫到原始文件。 –

回答

1

您可以將內容存儲在一個StringBuilder這樣的:

StringBuilder sbText = new StringBuilder(); 
using (var reader = new System.IO.StringReader(textFile)) { 
    while ((line = reader.ReadLine()) != null) { 
     if (line.Contains(stringToSearch)) { 
      //possibly better to do this in a loop 
      sbText.AppendLine(reader.ReadLine()); 
      sbText.AppendLine(reader.ReadLine()); 

      sbText.AppendLine("Your Text"); 
      break;//I'm not really sure if you want to break out of the loop here... 
     }else { 
      sbText.AppendLine(line); 
     } 
    } 
} 

,然後將它寫回這樣的:

using(var writer = new System.IO.StreamWriter(@"link\to\your\file.txt")) { 
    writer.Write(sbText.ToString()); 
} 

或者,如果你只是想將其保存在字符串textFile你可以這樣做:

textFile = sbText.ToString(); 
+0

非常感謝!這似乎是正確的做法。現在,我只有問題在將字符串寫回「textFile」字符串時出現錯誤。 「非法字符在路徑中」爲以下行: '使用(var writer = new System.IO.StreamWriter(textFile)){' – Giovanni19

+0

@ Giovanni19:我的錯誤,「textFile」應該是「鏈接到您的文本文件想寫信給「。我發現你在帖子中使用了不同的變量。如果你只是想將它作爲一個字符串存儲在'textFile'中,你可以使用:'textFile = sbText.ToString()'。我已經更新了答案。 – waka

+0

謝謝。但是,這增加了新線路到具有例如字符串「abc」。我想在字符串「abc」之後將新行添加到第三行。 – Giovanni19

0

寫整個文件再次會更容易些:

string old = "abc"; 
string nw ="aa"; 
int counter = 0; 

    Using(StreamWriter w =new StreamWriter("newfile"); 
    foreach(string s in File.ReadLines(path)) 
    { 
     if (s = old) 
     { 
       w. WriteLine(nw); 
     } 
     else 
     { 
       w. WriteLine(s); 
     } 
     counter ++; 
    } 
+1

現在行'abc'被替換,這不是OP想要的。 'new'是一個關鍵字,不能用作變量名稱。 – waka

0

你可能會想類似如下:

DirectoryInfo di = new DirectoryInfo(Location); 
FileInfo[] rgFiles = di.GetFiles("txt File"); 

foreach (FileInfo fi in rgFiles) 
{ 

    string[] alllines = File.ReadAllLines(fi.FullName); 


    for (int i = 0; i < alllines.Length; i++) 
    { 
     if (alllines[i].Contains(stringToSearch)) 
     {       
      alllines[i] = alllines[i].Replace(stringToSearch, some value); 
     } 
    } 
} 

這樣,您將通過線上閱讀的文本文件中的行,直到文檔的末尾,如果值被提取,它將被您的新值取代。

+0

這不是我想要的:/看到我的第一篇文章。 – Giovanni19

+0

@ Giovanni19,你想要逐行閱讀文檔並更新一個值(如果找到了),那麼你想更新這個值?我是對的?以上代碼將適用於此....是否要保存此文檔的新版本? – MrMime