2013-07-03 2 views
-4

좋아요, 저를 믿으십시오. 저는이 곳을 연구하여 몇몇 친구들에게이 문제에 대해 물었습니다. 하지만 내 코드는 http://pastebin.com/0JSA9MzA입니다. 지정된 두 줄 사이의 코드는 startLine 변수로 지정된 텍스트 행에 대해 텍스트 문서를 한 줄씩 검사하기로되어 있습니다. 그런 다음 oldFont 변수에서 newFont 변수로 모든 것을 변경 한 다음 endLine에서 끝내기로되어 있습니다. 변하기 쉬운. 그러나 나는 그것을 실행할 때마다 결과를 산출하지 못하고 어떤 오류도주지 않습니다.C# 텍스트 파일의 줄을 바꾸지 않겠습니다.

+2

코드의 관련 부분을 얻을 귀하의 질문에 그들을 포함하십시오. –

+0

-1 : 링크를 제공하는 대신 인라인 코드를 사용하십시오. "모든 곳에서 검색"할 필요가 없습니다. –

답변

2

파일의 텍스트를 변경하라는 메시지가 아니라면 코드가 파일의 텍스트를 변경하지 않습니다. 현재 읽고있는 텍스트 만 수정하고 있지만 파일에 다시 쓰지는 않습니다.

0

이 시도 :

private void editText() 
    { 
     System.IO.StreamReader read = new System.IO.StreamReader(filePath); 

     //write to new file because it's not possible to replace text in existing file directly 
     System.IO.TextWriter writer = new System.IO.TextWriter(filePath+"_"); 

     while ((line = read.ReadLine()) != null) 
     { 
      writer.WriteLine(line); //write unmodified line to new file 

      if (startLine == line) 
      { 
       while ((line = read.ReadLine()) != null) 
       { 
        if (line == endLine) 
        { 
         writer.WriteLine(line); //write unmodified line to new file 
         break; //exit cycle 
        } 
        else 
        { 
         line = line.Replace(oldFont, newFontS); //replace content in line 
         writer.WriteLine(line); //write modified line to new file 
        }      
       } 
      } 
     } 

     read.Close(); 
     writer.Close(); 

     System.IO.File.Delete(filePath); //delete old file 
     System.IO.File.Move(filePath+"_",filePath); //rename new file to original filename    


     MessageBox.Show("Done!"); 
    } 
+0

TextWriter를 사용할 수 없으므로 작동하지 않습니다. 따라서 StreamWriter로 전환 했으므로 작동하지 않습니다. –

+0

코드가 수정되었습니다. 그것은 작동합니다! –

관련 문제