Skip to content

Instantly share code, notes, and snippets.

@rubenwardy rubenwardy/form1.cs
Last active Aug 29, 2015

Embed
What would you like to do?
Clever browser opener in C#
/*
Made this script to automatically open a web page in the correct browser.
I use multiple browsers at the same time, so I don't want to keep changing my file associations.
(Firefox used to have association, so if I am creating a game in Chrome and open a link in IRC,
firefox is opened. With this, it will be chrome because it is the only one open)
It priorities Firefox over Chrome, but you can change that.
(I use Firefox for browsing, Chrome for HTML5 games.
I use both of these and IE for web site development).
License: WTFPL
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace CoFF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
openBrowser(Environment.GetCommandLineArgs()[1]);
Application.Exit();
}
public void openBrowser(string url)
{
bool found_chrome = false;
Process[] processes = Process.GetProcesses();
foreach (Process process in processes) {
//Get whatever attribute for process
if (process.ProcessName.IndexOf("firefox") != -1) {
// fire fox is open
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "firefox.exe";
startInfo.Arguments = url;
Process.Start(startInfo);
return;
} else if (process.ProcessName.IndexOf("chrome") != -1) {
found_chrome = true;
}
}
if (found_chrome) {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "chrome.exe";
startInfo.Arguments = url;
Process.Start(startInfo);
} else {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "firefox.exe";
startInfo.Arguments = url;
Process.Start(startInfo);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.