-
-
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'`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ModuleName; | |
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