How to use Application.Exit Event in WPF?

后端 未结 5 760
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 14:29

I need to delete some certain files, then user closes program in WPF. So I tried MDSN code from here http://msdn.microsoft.com/en-us/library/system.windows.application.exit.

5条回答
  •  没有蜡笔的小新
    2020-12-05 15:08

    you should add app_exit in your xaml code

     
    
    

    you can just hook the event Closing on your main window like this -

     
    

    And in your event do all the work you need

        private void Window_Closing(object sender, CancelEventArgs e)
    {
         MessageBox.Show("File deleted");
        var systemPath = System.Environment.GetFolderPath(
                                  Environment.SpecialFolder.CommonApplicationData);
    
                var _directoryName1 = Path.Combine(systemPath, "RadiolocationQ");
                var temp_file = Path.Combine(_directoryName1, "temp.ini");
    
                if (File.Exists(temp1_file))
                {
                    File.Delete(temp1_file);
                }
    }
    

提交回复
热议问题