c# - streamWriter 重写文件或附加到文件

标签 c# .net visual-studio visual-studio-2010

我正在用这个

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

}

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

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

使用 C#

最佳答案

试试 FileMode 枚举器:

        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

关于c# - streamWriter 重写文件或附加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7405828/

相关文章:

c# - 是否可以为 WebService 进行设置安装?

c# - 错误 : Cannot insert explicit value for identity column in table - INT to tinyInt

windows - 在 if 语句中使用多个操作时遇到问题

c# - C# 中的 Volatile 和 Thread.MemoryBarrier

c# - 使用 LINQ ForEach 循环的替代方法是什么?

.net - Directory.GetParent 中的错误?

c# - 如何覆盖 [] 数组中的 ToString()?

c# - 如何选择xyz空间中半径内的所有项目?

visual-studio - VisualStudio : translating a build version to a calendar date

.net - 在不使用 ReSharper 的情况下从 Visual Studio 中使用 dotPeek