Ejemplo n.º 1
0
        private void CloseWebView()
        {
            if (_webView2Control != null)
            {
                _webView2Control.Close();
                _webView2Control.EnvironmentCreated -= webView2Control1_EnvironmentCreated;
                _webView2Control.BrowserCreated     -= _webView2Control_BrowserCreated;
                _webView2Control.ContainsFullScreenElementChanged -= _webView2Control_ContainsFullScreenElementChanged;
                _webView2Control.NewWindowRequested -= _webView2Control_NewWindowRequested;

                _environment.UnregisterNewVersionAvailable(_newVersionToken);

                _settingsComponent.CleanUp();
                _settingsComponent = null;

                _fileComponent.CleanUp();
                _fileComponent = null;

                _processComponent.CleanUp();
                _processComponent = null;

                _scriptComponent.CleanUp();
                _scriptComponent = null;

                _controlComponent.CleanUp();
                _controlComponent = null;

                _viewComponent.CleanUp();
                _viewComponent = null;

                tableLayoutPanel1.Controls.Remove(_webView2Control);
                _webView2Control.Dispose();
                _webView2Control = null;
            }
        }
Ejemplo n.º 2
0
        private void CloseWebView(bool cleanupUserDataFolder)
        {
            if (_webView2Control != null)
            {
                _webView2Control.Close();
                _webView2Control.EnvironmentCreated -= webView2Control1_EnvironmentCreated;
                _webView2Control.BrowserCreated     -= _webView2Control_BrowserCreated;
                _webView2Control.ContainsFullScreenElementChanged -= _webView2Control_ContainsFullScreenElementChanged;
                _webView2Control.NewWindowRequested -= _webView2Control_NewWindowRequested;

                _environment.UnregisterNewVersionAvailable(_newVersionToken);

                _settingsComponent.CleanUp();
                _settingsComponent = null;

                _fileComponent.CleanUp();
                _fileComponent = null;

                _processComponent.CleanUp();
                _processComponent = null;

                _scriptComponent.CleanUp();
                _scriptComponent = null;

                _controlComponent.CleanUp();
                _controlComponent = null;

                _viewComponent.CleanUp();
                _viewComponent = null;

                tableLayoutPanel1.Controls.Remove(_webView2Control);
                _webView2Control.Dispose();
                _webView2Control = null;
            }

            if (cleanupUserDataFolder)
            {
                // For non-UWP apps, the default user data folder {Executable File Name}.WebView2
                // is in the same directory next to the app executable. If end
                // developers specify userDataFolder during WebView environment
                // creation, they would need to pass in that explicit value here.
                // For more information about userDataFolder:
                // https://docs.microsoft.com/microsoft-edge/hosting/webview2/reference/webview2.idl#createwebview2environmentwithdetails
                string userDataFolder = Environment.CurrentDirectory;
                // Obtain the absolute path for relative paths that include "./" or "../"
                userDataFolder = Path.Combine(userDataFolder, "MtrDev.WebView2.WinForms.Sample.exe.WebView2");
                string userDataFolderPath = userDataFolder;

                string message = "Are you sure you want to clean up the user data folder at\n";
                message += userDataFolderPath;
                message += "\n?\nWarning: This action is not reversible.\n\n";
                message += "Click No if there are other open WebView instnaces.\n";

                if (MessageBox.Show(this, message, "Cleanup User Data Folder", MessageBoxButtons.YesNo) ==
                    DialogResult.Yes)
                {
                    try
                    {
                        Directory.Delete(userDataFolderPath, true);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }