2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C#】 ファイルの内容を削除

Posted at

自己紹介

こんにちは。tetraです。
最近C#の学習を始めました。C#歴3ヶ月目の新卒エンジニアです。
ファイルを削除ではなく、ファイルの内容を削除するのはどうすればいいのかわからず時間を無駄にしたので作成します。

ファイルの削除

最初はデリートを押すなんて考えていましたが、ファイルサイズを0にすれば内容のみを削除できると考えました。
早速書いていきます。

sample.cs

using System.IO;
class sample
{
    static void Main()
    {
        using (var fileStream = new FileStream("./sample.txt", FileMode.Open))
        {
            fileStream.SetLength(0);
        }
    }
}

いかがでしょうか?

いかがでしょうか。経験が浅いため至らぬところがまだまだあります。
もし間違い等に気がつきましたら、コメント又はtetraまでお知らせください。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?