2017-02-17 87 views
-2
string line = string.Empty; 
int line_number = 1; 
int line_to_edit = 2; 

using (StreamReader reader = new StreamReader(@"C:\ut.txt")) 
{ 
    using (StreamWriter writer = new StreamWriter(@"C:\ut1.txt")) 
    { 
     while ((line = reader.ReadLine()) != null) 
     { 
      if (line_number == line_to_edit) 
      { 
       writer.WriteLine(line); 
      } 
      line_number++; 
     } 
    } 
} 

這是輸入文件ut.txt如何替換文本文件中特定的lat和long值?

1 
2 ----no. of splines 
6 0 --- 6 denotes no of lines in first spline 
365608.901276044 1288380.47235694 ----1st line 
365771.386298595 1288324.72422065 
366114.128536966 1288263.42618414 
366540.124442843 1288196.19420711 
367310.251273576 1288150.21544542 
368104.085736344 1288201.50959757----6th line 
4 0 ----4 denotes no of lines in second spline  
368825.120730879 1301890.60510298----6th line in first spline replace this line 
368712.400375608 1301704.1672357 
368741.48892057 1301615.81711783 
368740.370207785 1301488.64428575----1st line in first spline replace this line 

輸出需要的是:

1 
2 
6 0 
365608.901276044 1288380.47235694 
365771.386298595 1288324.72422065 
366114.128536966 1288263.42618414 
366540.124442843 1288196.19420711 
367310.251273576 1288150.21544542 
368104.085736344 1288201.50959757 
4 0 
368104.085736344 1288201.50959757 
368712.400375608 1301704.1672357 
368741.48892057 1301615.81711783 
365608.901276044 1288380.47235694 

回答

0

更新您的代碼如下

while ((line = reader.ReadLine()) != null) 
{ 
    if (line.Contains("Specific Lat/Long Value")) 
    { 
     line.Replace("Specific Lat/Long Value", "New Value"); 
    } 
} 

上面的代碼替換爲與更新字符串值。