Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted

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
 
Share this answer
 
You are using winforms? Handle the FormClosing event. The EventArgs contains the CloseReason.
 
Share this answer
 
Ok I find it.

You just use the
VB
FormClosing
event!
 
Share this answer
 
Comments
CHill60 8-Oct-14 17:58pm    
It's rather rude to repost someone else's solution as your own and then "accept" it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900