Create a gist now

Instantly share code, notes, and snippets.

@planetarian /Program.cs Secret
Last active Aug 11, 2017

What would you like to do?
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