Created
May 27, 2021 18:53
-
-
Save jpluimers/c38271933bdea49b1902fa88d568ab10 to your computer and use it in GitHub Desktop.
Code from answers to https://superuser.com/questions/241272/windows-how-to-redirect-file-parameter-to-stdout-windows-equivalent-of-dev-s
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.IO; | |
using System.IO.Pipes; | |
class PipeServer { | |
static int Main(string[] args) { | |
string usage = "Usage: PipeServer <name> <in | out>"; | |
if (args.Length != 2) { | |
Console.WriteLine(usage); | |
return 1; | |
} | |
string name = args[0]; | |
if (String.Compare(args[1], "in") == 0) { | |
Pipe(name, PipeDirection.In); | |
} | |
else if (String.Compare(args[1], "out") == 0) { | |
Pipe(name, PipeDirection.Out); | |
} | |
else { | |
Console.WriteLine(usage); | |
return 1; | |
} | |
return 0; | |
} | |
static void Pipe(string name, PipeDirection dir) { | |
NamedPipeServerStream pipe = new NamedPipeServerStream(name, dir, 1); | |
pipe.WaitForConnection(); | |
try { | |
switch (dir) { | |
case PipeDirection.In: | |
pipe.CopyTo(Console.OpenStandardOutput()); | |
break; | |
case PipeDirection.Out: | |
Console.OpenStandardInput().CopyTo(pipe); | |
break; | |
default: | |
Console.WriteLine("unsupported direction {0}", dir); | |
return; | |
} | |
} catch (IOException e) { | |
Console.WriteLine("error: {0}", e.Message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment