Skip to main content
 首页 » 编程设计

c#之在控制台应用程序中覆盖 txt 文件 C# 中的输出

2024年02月20日12jackei

if (string.IsNullOrEmpty(indata)) 
{ 
    StreamWriter sw = new StreamWriter(@"c:\arjun.txt", true); 
    sw.WriteLine("0"); 
    sw.Close();   
} 

这是我的代码。我需要单个结果,如何覆盖 arjun.txt 文件中的结果。

请您参考如下方法:

true 部分表示“追加” - 要么完全摆脱它,在这种情况下,您将使用 StreamWriter 构造函数重载,它会被默认覆盖,或者将 true 更改为 false。

或者最好只使用:

File.WriteAllLines(@"c:\argun.txt", new[] {"0"}); 

当您可以一次指定所有数据时,File 中的便捷方法真的很有帮助。