現在、テキスト ファイルから特定のテキスト行を削除する必要があるプロジェクトに取り組んでいます。
これは私がすでに持っているコードです:
static void Main( string[] args )
{
string line = null;
string line_to_delete = "--";
string desktopLocation = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string text = Path.Combine( desktopLocation, "tim3.txt" );
string file = Path.Combine( desktopLocation, "tim4.txt" );
using (StreamReader reader = new StreamReader( text))
{
using (StreamWriter writer = new StreamWriter(file ))
{
while((line = reader.ReadLine()) != null)
{
if (string.Compare( line, line_to_delete ) == 0)
continue;
writer.WriteLine( line );
}
}
これにより、テキストのみが新しい txt ファイルに書き込まれますが、何も削除されません。
ありがとう。主な問題は、txt ファイル内の特定のテキスト行を削除するだけでよいことです。