Skip to content

Instantly share code, notes, and snippets.

@soyprofesor
Last active April 23, 2024 11:04
Show Gist options
  • Select an option

  • Save soyprofesor/39ca6230cc53b1c47d2ae0f93d6c56c5 to your computer and use it in GitHub Desktop.

Select an option

Save soyprofesor/39ca6230cc53b1c47d2ae0f93d6c56c5 to your computer and use it in GitHub Desktop.
cmd read and write
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Create a new ProcessStartInfo
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe"; // Specify the command to run (in this case, cmd.exe)
psi.RedirectStandardInput = true; // Redirect input from the console
psi.RedirectStandardOutput = true; // Redirect output to the console
psi.UseShellExecute = false; // Don't use the shell to execute the process
psi.CreateNoWindow = true; // Don't create a window for the process
// Start the process
Process proc = Process.Start(psi);
// Use StreamWriter to write commands to the standard input stream of the process
using (System.IO.StreamWriter sw = proc.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("dotnet --version"); // Execute the 'dotnet --version' command
sw.WriteLine("exit"); // Exit CMD after executing the command
}
}
// Read the output from the standard output stream of the process
using (System.IO.StreamReader sr = proc.StandardOutput)
{
string output = sr.ReadToEnd();
Console.WriteLine(output);
}
// Wait for the process to exit
proc.WaitForExit();
}
}
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Create a new ProcessStartInfo
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "isignercli"; // Specify the command to run (in this case, dotnet)
psi.Arguments = "-uid"; // Specify the arguments for the command
psi.RedirectStandardOutput = true; // Redirect output to the console
psi.UseShellExecute = false; // Don't use the shell to execute the process
psi.CreateNoWindow = true; // Don't create a window for the process
// Start the process
Process proc = Process.Start(psi);
// Read the output from the standard output stream of the process
using (StreamReader sr = proc.StandardOutput)
{
string output = sr.ReadToEnd();
Console.WriteLine(output);
}
// Wait for the process to exit
proc.WaitForExit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment