Allow user input in CMD using C# [duplicate] - c#

This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed last year.
I wanted to run CMD commands using C#. Therefore I copied some Code:
using System.IO;
using System.Diagnostics;
namespace Test
{
internal class Program
{
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("#echo off");
sw.WriteLine("title Test");
}
}
}
}
}
So. After I run some code like changing color etc, I want to enter normal cmd commands myself but the Window instantly closes.
Thanks for any Help :D

Use ReadKey()
Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed.

Related

Is there a way to have c# open another program using events? [duplicate]

This question already has answers here:
Launching an application (.EXE) from C#?
(9 answers)
Closed 4 years ago.
For example: if one program had an alert, another separate program pop up when that alarm goes off?
Yes, you can launch another program using C# code. Here's how you do that:
static void LaunchProgram()
{
// Path to program
const string ex2 = "C:\\Dir";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
//File name of program to launch
startInfo.FileName = "program.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}

Execute command in current terminal, without opening up a new terminal.

I have a c# application currently running from the Terminal. It opens up a new terminal to execute a certain command then closes that terminal. I'm wondering if I can just execute this command in the terminal that is currently opened, instead of opening a new one. My code to execute the command is as follows.
Process proc = new System.Diagnostics.Process();
proc.StartInfo.WorkingDirectory = #"MyDirectory";
proc.StartInfo.FileName = #"/usr/bash";
proc.StartInfo.Arguments = command;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
How would I rewrite this without opening a new terminal?
As far as I know, no - but you can use the code
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
to make it so that the window does not pop up. Then in your normal terminal you can use print to update the user with a message like "Running Script..."
You can directly use libc system vs. the Diagnostics.Process and the redirection pipes (which just wrap the libc functions):
using System;
using System.Runtime.InteropServices;
namespace ShellMe
{
class MainClass
{
[DllImport ("libc")]
private static extern int system (string exec);
public static void Main (string[] args)
{
system("YourCommand2Run"); // blocking, you are in a shell so same rules apply
system("YourCommand2Run &"); // non-blocking
}
}
}

externally access processing via cmd

As I aksed in another post, I am trying to automate running processing ide from c#. Finally I found the way to run the processing sketch via cmd, with setting the installed processing folder in the path of evironment variable.
I find it works with inputting command directly in cmd.exe, but when I want to do the same thing through some c# code in Visual Studio, it doesn't run the .pde file.
Here is the code,
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Runprocessing
{
static void Main()
{
Process process = new Process();
ProcessStartInfo stinfo = new ProcessStartInfo();
stinfo.FileName = "cmd.exe";
stinfo.Arguments = "/c"+"processing-java --run --sketch=D:\\pw --output=D:\\pw\\output";
stinfo.CreateNoWindow = true;
stinfo.UseShellExecute = false;
process = Process.Start(stinfo);
process.WaitForExit();
process.Close();
process.Dispose();
}
}
}
My question is, how should I properly use processing-java to activate the sketch. because here I am stating
stinfo.FileName = "cmd.exe";
stinfo.Arguments = "/c"+"processing-java --run --sketch=D:\\pw --output=D:\\pw\\output";
Is this the right way to use processing-java in cmd?

Run an another cmd [duplicate]

This question already has answers here:
Run console application from other console app
(6 answers)
Closed 9 years ago.
I make a C# console program and I want to execute some batch treatments in an other cosole.
So, I have the main program which write in the console and at a certain moment I want to execute batch treatment in an other one.
I know how execute batch treatment in the main console but I want to do that in an other one, that is my question.
How can I make that ?
EDIT :
I use a StreaWriter to write in the console like that :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
using (StreamWriter writer = process.StandardInput)
{
if (writer.BaseStream.CanWrite)
{
// commands...
}
}
Use Process.Start:
Process.Start("cmd.exe", "yourcommandhere");

p4 opened -a in C# Perforce Wrapper or P4 API

I found this thread very helpful and I would like to ask Ian Norton about his wrapper. HERE is the link to the wrapper I was trying that IanNorton had posted. I'm not sure if this is the right place to ask and I also don't want to create a new thread when it pertains to his response. So I will go ahead and suffer whatever backlash may come my way.
I am currently trying to use your wrapper and i cannot seem to seem to get it to trigger anything when I run it. I do not want to use options as i just want to set this as an .exe that runs on a timer. Quite simply, I would like to use the p4 opened -a cmd and print the out puts to a file. That is it. Any help would be greatly appreciated by this NooB.
Thank you very much!
This is as far as I got with just using the Command Line. Unfortunately I could not output my info to a text file.
using System;
using System.Diagnostics;
using System.IO;
namespace P4OpenCMD
{
class P4Opened
{
// Main begins program execution.
static void Main(string[] args)
{
string temp = string.Empty;
if (temp != string.Empty)
{
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
StreamWriter sw = p.StandardInput;
using (sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("set P4PORT=####");
sw.WriteLine("set P4USER=####");
sw.WriteLine("set P4CLIENT=####");
sw.WriteLine("set P4PASSWD=####");
sw.WriteLine("p4 opened -a //Depot/...");
sw.WriteLine("pause;");
}
Console.WriteLine();
}
sw.Close();
p.WaitForExit();
p.Close();
}
}
}
}
If you do this, you should be in good shape:
info.FileName = "cmd.exe";
info.Arguments = "/c p4 opened > opened.txt";

Categories