Skip to content

Instantly share code, notes, and snippets.

@planetarian
Last active August 11, 2017 08:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save planetarian/048b2475dfd9edea65e2097e851d6431 to your computer and use it in GitHub Desktop.
Save planetarian/048b2475dfd9edea65e2097e851d6431 to your computer and use it in GitHub Desktop.
Simple C# app which runs whatever the user specifies. Place in System32, then in WSL do `alias open='open.exe'`
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
namespace Run
{
class Program
{
static void Main(string[] args)
{
string exeName = Process.GetCurrentProcess().MainModule.Mo‌​duleName;
if (args.Length < 1)
{
Console.WriteLine($"Usage: {exeName} <command> [parameters ...]");
return;
}
var ps = new ProcessStartInfo(args[0]);
if (args.Length > 1)
{
string[] newArgs = args.Skip(1).ToArray();
ps.Arguments = String.Join(" ", newArgs);
}
try
{
Process.Start(ps);
}
catch (Win32Exception ex) when (ex.Message == "Element not found")
{
Console.WriteLine($"Error opening file {args[0]}:");
Console.WriteLine("No default application has been registered for that type.");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine($"Error starting process {args[0]}:");
Console.WriteLine($"{ex.Message} ({ex.GetType()})");
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment