如何在计算机关闭时自动保存文件? [英] How do I save a file automatically when computer shutting down?

查看:147
本文介绍了如何在计算机关闭时自动保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



当用户按下计算机上的关机按钮时,我想知道自动保存txt文件的代码, Vb.Net 代码......如果有办法的话。



我之所以放置自己 - 现在你 - 认为这样的代码是因为我编写了一个记事本,我想尽量减少包含5000个字符的文本的丢失,如果用户忘记保存它。为此他每次按下机器上的按钮时都会有程序为他做这件事。



你可以使用任何目的地并命名你想要代码,我稍后会改变它。 ;)



提前致谢,

GeorgeGkas

Hello guys,

I want to know the code that saves my txt file automatically when the user press the shut down button on his computer, with Vb.Net code....if there is a way.

The reason why I put myself -and now you- to think such a code is because I programed a Notepad and I want to minimize the lost of a text that contains 5000 characters if the user forget to save it. And for that he will have the programm to do it for him every time he press the sut-down button on his machine.

You can use any destination and name you want for the code,I will change it later. ;)

Thanks in advance,
GeorgeGkas

推荐答案

你可以使用类 Microsoft.Win32.SystemEvents 响应此系统事件或其他系统事件。请参阅:

http ://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents%28v=vs.110%29.aspx [ ^ ]。



例如,在处理事件 SystemEvents.SessionEnding 时,您可以检查调用此事件的原因。如果可以是 SessionEndReasons.Logoff SessionEndReasons.SystemShutdown

http://msdn.microsoft.com/en-us/ library / microsoft.win32.systemevents.sessionending%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendingeventargs% 28v = vs.110%29.aspx [ ^ ],

HTTP:// MSDN .microsoft.com / zh-CN / library / microsoft.win32.sessionendingeventargs.reason%28v = vs.110%29.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendreasons%28v=vs.110%29.aspx> http://msdn.microsoft.com/en-us /library/microsoft.win32.sessionendreasons%28v=vs.110%29.aspx [ ^ ]。



-SA
You can respond to this or other system events using the class Microsoft.Win32.SystemEvents. Please see:
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents%28v=vs.110%29.aspx[^].

For example, while handling the event SystemEvents.SessionEnding, you can check up the reason of the invocation of this event . If could be either SessionEndReasons.Logoff or SessionEndReasons.SystemShutdown:
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendingeventargs%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendingeventargs.reason%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendreasons%28v=vs.110%29.aspx[^].

—SA


您正在使用winforms?处理FormClosing事件。 EventArgs包含CloseReason。
You are using winforms? Handle the FormClosing event. The EventArgs contains the CloseReason.


好的我找到它。



你只需使用
Ok I find it.

You just use the
FormClosing

事件!


这篇关于如何在计算机关闭时自动保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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