Click here to Skip to main content
14,825,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am working on a addin for Outlook now. We need to keep user credentials for this addin. We have used Registry to keep these details.

is there a way to delete, this registry details during system shutdown ?
Updated 28-Sep-14 23:32pm
Mehdi Gholam 29-Sep-14 3:41am
Your process will have to be running in the background, and detect a SHUTDOWN event.
BillWoodruff 29-Sep-14 3:51am
"Shutdown:" of your application, of the user's computer ?
Kiran Wilson 29-Sep-14 4:05am
Computer shutdown..

To make this answer perhaps more generally useful, I will assume here that the application wants to give the user a choice about whether to shut-down the machine, or not. That's an expansion of the topic beyond what the OP has asked.

Try this (my corrected version of Microsoft's incorrect example of using 'SessionEnding) [^]:
private static int WM_QUERYENDSESSION = 0x11;

private static bool systemShutdown = false;

protected override void WndProc(ref System.Windows.Forms.Message m)
    if (m.Msg == WM_QUERYENDSESSION)
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
        systemShutdown = true;

    // If this is WM_QUERYENDSESSION, the closing event should be
    // raised in the base WndProc.
    base.WndProc(ref m);


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    if (systemShutdown)
    // Reset the variable because the user might cancel the 
    // shutdown.
        systemShutdown = false;
        if (DialogResult.Yes == MessageBox.Show("My application",
            "Do you want to save your work before logging off?",
            // your code to write/modify registry goes here ...
            e.Cancel = true;
            e.Cancel = false;
But, you may not be satisfied with the result. If you use this in Win 8.1, for example, assuming your application is running, when the user shuts-down the computer:

1. Some applications (not yours) are going to get shut-down before the shut-down process is halted by the code shown above.

2. The shut-down will halt at a blue-screen with information about what is preventing the computer from shutting-down, and your program will be among the items listed.

3. from that blue-screen the user can either cancel and return to whatever state is now current (remember that some applications may have been shut-down): if you cancel the shut-down, you will then see your dialog box. From that blue-screen any blocking application can be forced to close.

Other things to keep in mind:

1. MS cautions you ... re this technique: "Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result." But, as often with MS docs, there's no practical example given of this.

2. The end-user could have shut-down your application using the Task Manager. Perhaps that's not important here, and you can handle that in the 'FormClosing EventHandler by evaluating if e.CloseReason == CloseReason.TaskManagerClosing
Sinisa Hajnal 29-Sep-14 6:33am
Very detailed. My vote of 5.
Use Application.ApplicationExit event[^]

Just saw Bills question: is it on COMPUTER shutdown?
Then use SystemEvents.SessionEnded[^] - or Ending if you want to be able to cancel pending shut down...
BillWoodruff 29-Sep-14 4:59am
fyi: the MS code example for 'SessionEnding ... which is what the OP almost certainly would want to use ... is incorrect. And, in Win 8/8.1 may not meet the OP's needs, anyway (see my response here).
Sinisa Hajnal 29-Sep-14 5:02am
OS: Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

Didn't really take a look at Ending as he asks for deleting registry, just mentioned it as potential interest.
BillWoodruff 29-Sep-14 5:42am
My comment was not meant as a criticism, and was based on a general conclusion ... after years of WinForms programming ... that it is always better to do anything "extra" in a 'Closing Event, compared to using 'Close. It may well be the case the OP here can do exactly what they want to do in the 'Close Event.
Sinisa Hajnal 29-Sep-14 6:31am
I didn't take it as criticism, I was answering the part about Win sounded like the solution is limited to that OS when it isn't.

Sorry if it came out wrongly.

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