2

I need to run some code after my application is exited (it's a C# and WPF application, developped with VisualStudio).

Basically, when an update is available, the application should exit, copy new files (from our server to the user computer), and open again (of course, after user confirmation, and all other checks).

I am able to manage the update, but when I copy new files, I have an error saying some files are in use (which is indeed normal).

How can I do that ?

For example, my "update" method is as follow :

            try
        {
            // Need to close the app here i guess

            // File copying : it's ok here

            // Open the app again
        }
        catch (Exception e)
        {
            MessageBox.Show("Cannot update\n" + e.Message, "MyApp", MessageBoxButton.OK, MessageBoxImage.Error);

            Environment.Exit(1);
        }

Thank's a lot for helping me to perform this task.

Rems
  • 119
  • 8
  • 2
    On exit from the first application, start another process that does the update and then restarts the first process when it's done. – 15ee8f99-57ff-4f92-890c-b56153 Jun 04 '19 at 14:26
  • 2
    Can't you execute the update application right before you quit your main application, use the update application to carry out the update and then start the main application back up right before you quit the update application? – Nico Jun 04 '19 at 14:26
  • Possible duplicate of [How to use Application.Exit Event in WPF?](https://stackoverflow.com/questions/20346746/how-to-use-application-exit-event-in-wpf) – Owen Pauling Jun 04 '19 at 14:26
  • 1
    Why not use an existing solution, like this: https://github.com/ravibpatel/AutoUpdater.NET – felix-b Jun 04 '19 at 14:29

1 Answers1

0

There isn't any way of having the program execute code once it is exited. I would say there are 3 sensible options of doing this.

  1. Have your program go into an "update state" where it basically closes all files/streams and everything else, then copies the files over. You shouldn't have any external files open so the files in use error won't be there.
  2. Have a seperate updating program. Basically once you try to update in your main program, this will launch the updater then close the main program once that has loaded. Once the updater has copied all the relevant files then the main program can be launched again.
  3. As commented by felix-b, there are a number of existing solutions which will handle the updating of your program. Such as https://github.com/ravibpatel/AutoUpdater.NET or just google .NET application updater.
Tom Dee
  • 2,516
  • 4
  • 17
  • 25
  • Hello. Autoupdater works fine. But I have another question please. Do you know how I can specify an UNC path inside node ? The application crash. But if I upload my app on a web server, it's ok. Our puropose here, is to have the app on a file server. – Rems Jun 05 '19 at 06:57
  • @Rems I'm afraid I don't have much experience with it so I'm not 100% sure. You can have a look at the documentation on github or maybe ask another question on here if you can't find it? – Tom Dee Jun 05 '19 at 14:48