gpt4 book ai didi

c# - c# 退出应用程序时如何清除txt文件

转载 作者:行者123 更新时间:2023-11-30 17:43:20 27 4
gpt4 key购买 nike

这是我的代码,用于填充我的 txt 文件,并显示在我的应用程序中。

StreamWriter file = new StreamWriter("opslag_kentekens",true);
string opslag_kentekens = textBox1.Text;
file.WriteLine(opslag_kentekens);
file.Close();

label20.Text = File.ReadAllText("opslag_kentekens");

我的问题是:退出应用程序时如何清除 txt 文件?

最佳答案

清除文本文件非常简单:只需向其中写入空字符串即可:

StreamWriter file = new StreamWriter("opslag_kentekens", false);
file.Write(String.Empty);
file.Close();

捕获应用程序退出取决于您在应用程序中使用的框架。

例如,在 WinForms 上(看起来您正在使用它)您可以覆盖主应用程序表单的 OnFormClosed 方法并在那里清除文件:

protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);

//clear your file
}

或者您可以处理 Application.ApplicationExit 事件并从那里清除您的文件。

关于c# - c# 退出应用程序时如何清除txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31071177/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com