How to close browser window in c#

How to Close Browser Window in C#

To close a browser window in C#, you can use the window.close() method in JavaScript. C# does not have a direct method to close a browser window, but you can execute JavaScript code using C#.

Here’s an example:

string script = "window.close();";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindowScript", script, true);

In the above example, we first create a string variable called script and assign it the value "window.close();". This JavaScript code will close the current browser window.

Then, we use the RegisterStartupScript method of the ClientScript class to register the JavaScript code to be executed on the client-side. The first argument is the type of the current instance, which you can replace with your own class type. The second argument is a unique key to identify the script, and the third argument is the script itself. The fourth argument, true, specifies that the script should be wrapped in a script tag for execution.

When this code is executed, the browser window will be closed.

Leave a comment