2016-08-22 5 views
1

既存のファイルの正確な場所にテキストを挿入しようとしています。 最初に適用した方法 - 何も影響しませんか?既存のテキストファイルの正確な場所にテキストを挿入する

 string newData; 
     string data = System.IO.File.ReadAllText("lal.txt"); 
     int indx = data.IndexOf("1"); 
     if (data.Contains("1")) 
     { 
      MessageBox.Show(indx.ToString()); 
      newData=data.Insert(indx+1, "ooooooooooooooooooooooooo"); 
      File.WriteAllText("lal.txt",data); 

     } 

私は - このメソッドを使用しているもう一つの方法は、完全にすべての内容を消去したり全くコンテンツを作成していません?

string newData; 
     string data = System.IO.File.ReadAllText("lal.txt"); 
     int indx = data.IndexOf("1"); 
     if (data.Contains("1")) 
     { 
      MessageBox.Show(indx.ToString()); 
      newData=data.Insert(indx+1, "ooooooooooooooooooooooooo"); 

      var f = new StreamWriter(File.Create("ll1.txt")); 
      f.Write(newData); 
      //data.Insert(indx, "OIasasas"); 
     } 

答えて

0

最初のスニペットにはエラーがあります。変更された文字列はnewData変数にありますが、変更されていない文字列をファイルに書きました。

2番目のスニペットには別のエラーがあります。適切な文字列を書きますが、ファイルストリームを閉じないでください。変更が行われない可能性があります。

これはworked..nicelyことのthnx

string data = System.IO.File.ReadAllText("lal.txt"); 
int indx = data.IndexOf("1"); 
if (indx != -1) 
{ 
    var newData = data.Insert(indx + 1, "ooooooooooooooooooooooooo"); 
    File.WriteAllText("lal.txt", newData); 
} 
+0

を動作するはずです – mofidul

関連する問題