streamWriter 重写文件或附加到文件 [英] streamWriter rewrite the file or append to the file

查看:24
本文介绍了streamWriter 重写文件或附加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个

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

}

如果 test.text 中有内容,此代码不会覆盖它.我有两个问题

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

这篇关于streamWriter 重写文件或附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆