关闭Windows窗体时如何删除文本文件? [英] How to delete a text file when i close my windows form?
本文介绍了关闭Windows窗体时如何删除文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为应用程序中的某些进程使用文本文件.当我关闭应用程序时,也需要关闭文本文件.如何执行此操作?请告诉我您的建议.
谢谢您,
Hi,
I am using text file for some processes in my application.When i am closing my application the text file also need to be closed.How to do that?Kindly tell me your suggestions.
Thank you,
推荐答案
有 Form_Closing 事件.用来删除您的txt文件.
There is Form_Closing event. Used that to delete your txt file.
要删除该文件,请使用File.Delete(string filepath)
方法.
To delete the file, use File.Delete(string filepath)
method.
您可以使用已关闭事件:
如果您有 FileStream ,并且您希望在表单已关闭,您可以使用设计器或在表单的构造函数中,将事件处理程序添加到Closed
事件中,如下所示:
If you have a FileStream and you want to close it when the form is closed, you can add an event-handler to the Closed
event using the designer or, in the constructor of your form, like the following:
this.Closed += new EventHandler(Form1_Closed);
并实现事件处理程序以关闭文件,如下所示:
and implement the event-handler to close your file, like the following:
void Form1_Closed(object sender, EventArgs e)
{
myFileStream.Close();
}
这篇关于关闭Windows窗体时如何删除文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文