Reputation:
I'm making some kind of web opener.
So basically if you type a link e.g. www.google.com/123456 , it will delete the last two characters and should add new ones ( I need to implement this).
My problem right now is, I can't open any link at all.
I've seen on stackoverflow people are using Process.Start()
, so I've tried it but it says that, the system can't find the file specified.
static void Main(string[] args)
{
Console.WriteLine($"Type your link here: ");
string url = Console.ReadLine();
url = url.Substring(0, url.Length - 2);
GoToSite(url);
Console.ReadKey();
}
public static void GoToSite(string url)
{
Process.Start(url);
}
I expect to open the link, and the if the input is www.google.com/123, the output should be www.google.com/1
Upvotes: 0
Views: 97
Reputation: 437
Process.Start(string url)
works differently on .NET Core than on .NET Framework.
For .NET Core you need to do it like this:
public static void GoToSite(string url)
{
try
{
Process.Start(url);
}
catch
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
throw;
}
}
}
Source: https://brockallen.com/2016/09/24/process-start-for-urls-on-net-core/
Upvotes: 2
Reputation: 56
I think this is what you are looking for?
make sure you have chrome installed, and In global os(windows) path var full path of chrome is setup.
then you simply call Process.Start("chrome.exe")
which starts google chrome.
static void Main(string[] args)
{
Console.WriteLine($"Type your link here: ");
string url = Console.ReadLine();
url = url.Substring(0, url.Length - 3);
GoToSite(url);
Console.ReadKey();
}
public static void GoToSite(string url)
{
string chromeArgs = $"--new-window {url}";
Process.Start("chrome.exe", chromeArgs);
}
Upvotes: 0