How to open new browser window in C#, WinUI 3? - c#

I am trying to open a URL in a new browser window. Specifically, a new browser window. The following code launches the URL in the default browser, but always opens the URL as a new tab in an existing browser window (if one exists). I want to launch the new tab in a new browser window, regardless of if one (or more) browser windows are already open.
This code launches the URL in WinUI 3 (in a new tab in an existing browser window), and appears to be the simplest:
Windows.System.Launcher.LaunchUriAsync(new Uri("http://google.com"));
This code does the same thing, and also works in WinUI 3 (credit to this answer here):
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = "http://google.com";
myProcess.Start();
Please note that the following code does NOT work at all in WinUI 3:
System.Diagnostics.Process.Start("http://google.com");
EDIT: Additional Ruled-out Method
The following modification of Process.Start() works in WinUI 3, but like the above methods only opens new tabs in existing browser windows, and thus is not a solution to the question above (also note that it forces using a specific browser, rather than the default browser):
Process.Start("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe", "http://google.com");

You can do it by calling a JavaScript command into webview2:
await webView.ExecuteScriptAsync("window.open('http://www.bing.com');");
Make sure you have either set the source of webView, or that you have already called EnsureCoreWebView2Async().

Related

using C# Process.Start("msedge", "--app=\"https://www.google.com\"") opens in blank browser window

I'm developing .NET Windows From application in C#. As per my requirement, I need to open a URL in the browser's Pop up (not the browser main window) same as it is opened using javascript's "window.open" method. This was a little tricky but I managed by using the below CMD command.
C:\Window\system32>start msedge --app="https://www.google.com/"
I parsed the above command into C# code to trigger a similar result. Process.Start("msedge", "--app=\"https://www.google.com\"");
The code works fine for Chrome only. But when it runs for Edge or Firefox it opens the browser (main browser window) with an empty address bar. Whereas I'm expecting this should open the URL in the mentioned browser popup window.
Which version of Edge are you using? I use code below and it works well. You can try it to see if it works on your machine:
Process process = new Process();
string arg = "--app" + "=" + "https://www.google.com";
process.StartInfo.FileName = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
process.StartInfo.Arguments = arg;
process.Start();

Multiple IE web-drivers : session lost when click link/button that opens a popup window

I run multiple (2) IE web-drivers together.(for user side and admin side tests)
When the InternetExplorer WebDriver click on link/button that opens an modal popup, login page are opened on new window (abnormal).
When the ie webdriver popup the window , I can see for a fraction of a second the correct address the browser should load (in URL line) , but immediatly it changed to the login url, and required enter username and password.
I found the same problem in this link,
I made the solution there, but I still have not solved the problem, and I get an entry window instead of the expected window.
see this screenshot
My IE version is 11.
My Selenium.WebDriver is 3.141.0.0
My ie webdriver is IEDriverServer_x64_3.141.5
My InternetExplorerOptions:
private static InternetExplorerOptions ieOptions = new InternetExplorerOptions
{
EnsureCleanSession = true ,
EnableNativeEvents = true ,
RequireWindowFocus = true ,
EnablePersistentHover = true ,
ForceCreateProcessApi = true ,
BrowserCommandLineArguments = "-framemerging -private"
};
I added the registry keys:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FrameMerging\(DWORD)00000000
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\TabProcGrowth\(DWORD)00000000
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE\iexplorer.exe\(DWORD)00000000
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE\iexplorer.exe\(DWORD)00000000
Edited:
By the Deepak-MSFT references, I understand that the issue caused by more than one IE web-driver instances.
I tried to kill the IE driver also by adding the ConfirmCleanSession = trueoption to the code, and also manually by running of the following statements from the command line (before running the test);
taskkill / F / IM iexplore.exe / T
taskkill / F / IM IEDriverServer.exe / T
But it did not help once I set 2 IE web-drivers to work together.
(When I tried to run the test only on one driver - it worked great!)
I would appreciate help in solving the problem!
This issue is due to a quirk of IE itself and how the driver creates the popup window for the browser.
The workaround is to ensure there are no iexplore.exe processes running when you start the session with the driver.
References:
(1) session cookie lost when click made to a link that opens a window with window.open()
(2) session cookie lost when click made to a link that opens a window with window.open()
My Solution:
Split the test case into two tests:
First Test -
Use the IE web-driver for the first site (in my case - the admin side), and parallel use another web-driver (which does not cause problems like Chrome web-driver) for the second site (in my case - the admin side)
And a second test-
Use the other web-driver above for the first site, and parallel use the IE web-driver for the second site.
So I covered the tests in IE for the two sites.

When debugging, is there any way to get Visual Studio to load DLLs for my Web API project without opening a browser window

I can make Visual Studio open my ASP.NET Web API in a new browser window, but that's annoying because I don't want a browser window to pop up. I want to go straight to Postman and start hitting endpoints.
Ideally I'd like to have Visual Studio try to run an endpoint (e.g. http://localhost:54292/token) silently in the background, which will trigger the loading of most of the DLLs I need automatically.
I know I can use the "start external program" feature and use something like cURL, but this opens a command window in the foreground, which is also annoying.
Is there any way to hide the command window, run my program minimized, or make it eager load DLLs when debugging?
Choose the option "Don't open a page. Wait for a request from an external application", then use Postman after starting your debug session.
Alternatively, write a new windows form application and set that as the startup project. In that application, call the url you are interested in and remove the default form. It will have no GUI.
You could also have the web application itself make a call into itself on startup by putting that code in your startup (globax.asax or startup.cs). You would probably want to remove this on deploy though, hence why I'd suggest a separate application.
please try below line of code if it helps you
var proc1 = new ProcessStartInfo();
proc1.FileName = #"cmd.exe";
proc1.Verb = "runas";
proc1.Arguments = "/c " + command;
proc1.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(proc1);

Windows desktop application open URL in a new browser window instead of a new tab

I am using Process.Start(url) to open a url in a new tab in user's browser. However, I want it to be opened in a new browser window instead of a new tab. Thanks in advance for your help.
You can use the overloaded Process.Start(string, string) which will take command line arguments. Firefox and Chrome both have a command line switch to specify a new window. You'll need to figure out what Browser the client has, and use the appropriate command line args.

open specific file in appropriate app from inside Windows 8 App

I'm creating Windows 8 app in which i need to list specific files in a page and then let user open that file. I'm using C# as backend.
I want to ask this (as in the image below), to my user when they try to open any app listed in my app page.
when user select specific app, then the file that is clicked should open in that app.
Unlike previous versions of Windows, the User is now in control of file associations.
To show the launch using the Window above, you can refer to the complete example of launching options here.
The core of it is deciding on a Point on the screen (which is openWithPosition in the example below) and then calling LaunchUriAsync with the options set to display the window.
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
options.UI.PreferredPlacement = Windows.UI.Popups.Placement.Below;
// Launch the URI.
bool success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
The linked example code contains a function that can compute a reasonable Point given a XAML element.

Categories

Resources