As horrible as it sounds, you can leverage Microsoft.VisualBasic.ApplicationServices to make this really simple (you can add a reference to Microsoft.VisualBasic in your c# project).
As a quick example, you can create a new C# WinForms project and alter Program.cs to look something like this:
class Program : WindowsFormsApplicationBase
{
static Form1 mainForm = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] commandline)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program prog = new Program();
prog.MainForm = mainForm = new Form1();
prog.Run(commandline);
}
public Program()
{
this.IsSingleInstance = true;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
base.OnStartupNextInstance(eventArgs);
mainForm.Startup(eventArgs.CommandLine.ToArray());
}
}
Then in the Form1 throw a label on there and a little code to show it's working:
public void Startup(string[] commandLine)
{
string output = "";
foreach (string arg in commandLine)
output += arg + "\n";
label1.Text = output;
}
public Form1()
{
InitializeComponent();
Startup(Environment.GetCommandLineArgs());
}
The only gotcha with this little snippet is that the command line arguments you get on first launch include the application name, but it's not included on subsequent launches.