的StreamWriter重写文件或追加到该文件文件、重写、到该、StreamWriter

2023-09-03 01:28:00 作者:ʚ過期關係꧔ꦿ<

我用这

for($number=0; $number < 5; $number++){
StreamWriter x = new StreamWriter("C:\\test.txt");
                x.WriteLine(number);
                x.Close();

}

如果事情是在test.text,这code不会覆盖它。我有2个问题

if something is in test.text, this code will not overwrite it. I have 2 questions

1: how can I make it overwrite the file
2: how can I append to the same file

使用C#

推荐答案

尝试的FileMode枚举:

Try the FileMode enumerator:

        FileStream fappend = File.Open("C:\\test.txt", FileMode.Append); // will append to end of file

        FileStream fcreate = File.Open("C:\\test.txt", FileMode.Create); // will create the file or overwrite it if it already exists
 
精彩推荐