I try to run a process.start() for command in command line, and try to get the output into string or some usefull locaiton. The output will consist of several rows ( like DIR command ). I read how to do it but it doesn' t work for me. It runs but then got into loop and does not stop.See below. any ideas?
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("cmd.exe",
#" /k dir");
Process myProcess = new Process();
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = #" /k dir";
myProcess.Start();
string ppp = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
Your process will never exit on its own because you're passing the /K flag to cmd.exe which will keep the window open (and hence the process alive) after it runs your command.
/K Carries out the command specified by string but remains
I think what you're looking for here is to use /C which will close the command window after it runs your command.
/C Carries out the command specified by string and then terminates
Related
In c#, run new process cmd and send out command(ex.. ping 127.0.0.1 -t) to cmd
Then redirect Standardoutput to Console..
But in compile I cannot see cmd which is working in pop up window, only know that command is working on hidden state.
How can I show up command prompt like console pop up?? I want to see console window and command prompt at the same time..
If I don't redirect from cmd and only send the command(ex dir) to cmd,
still command prompt window does't showed up and only see console window..
Process proc_cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"c:\";
startinfo.UseShellExecute = false;//to use RedirectStandard~
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
proc_cmd.EnableRaisingEvents = false;
proc_cmd.StartInfo = startinfo;
proc_cmd.Start();
proc_cmd.StandardInput.Write(#"ping 127.0.0.1 -t" +
Environment.NewLine);
proc_cmd.StandardInput.Close();
type start in command prompt:
start
This will open up a new cmd window.
You may need something like this if you need to view the console on debug:
startInfo.CreateNoWindow = false;
I have a console app that does a number of things. One of the steps involves calling an EXE (AzCopy). I've successfully run this type command in a prompt and it looks like this:
AzCopy /Source:https://mysite.table.core.windows.net/MYTABLE/ /SourceKey:SOURCEKEY /Dest:C:\export /Manifest:MYTABLE
I need to run this script in the middle of a console app's execution. I've seen a number of different ways to do this, but none seem to work. I've tried this:
System.Diagnostics.Process.Start("CMD.exe", command);
And this:
var procStartInfo = new ProcessStartInfo("AzCopy", command);
And this:
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
None seem to actually kick off the AzCopy call. What gives?
System.Diagnostics.Process.Start("CMD.exe", command); should work.
But you need to add /C at the beginning of your command.
Something like this:
"/C AzCopy /Source:https://mysite.t........................................."
I am currently trying to "convert" a bash script I wrote to C#.
This script starts a program in a shell and then executes a few commands in this shell and looks similar to this:
cd /$MYPATH
./executible -s << EOF
start_source_probe -hardware_name "USB" -device_name "#1: EP3C(10|5)"
write_source_data -instance_index 0 -value "11111"
write_source_data -instance_index 0 -value "10111"
write_source_data -instance_index 0 -value "00111"
exit
EOF
Now I would like to do the same using Visual Studio C#.
At the moment my attempt looks like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = "pathToExe\\";
startInfo.FileName= "executible.exe";
startInfo.Arguments= "-s";
//startInfo.UseShellExecute = false;
//startInfo.RedirectStandardInput = true;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
//StreamWriter myStreamWriter = proc.StandardInput;
//Console.WriteLine("start_source_probe - hardware_name \"USB\" -device_name \"#1: EP3C(10|5)\"");
//Console.WriteLine("write_source_data -instance_index 0 -value \"11111\"");
proc.WaitForExit();
With the comments activated (so with the "//" in code) I manage to open the shell (-s stands for shell) but I wonder how I am able to execute commands in this shell additionally.
I managed to execute multiple commands with something like this (as long as I am not starting the shell because destination output differs I guess)
const string strCmdText = "/C command1&command2";
Process.Start("CMD.exe", strCmdText);
I would appreciate it if someone could tell me how to add the argument "-s" and execute commands in the started process.
I am not sure if i did understood your question, but you can create a batch file outside your c# code and call It from your c# code like the following :
System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.UseShellExecute = false;
ProcStartInfo.CreateNoWindow = false;
ProcStartInfo.RedirectStandardError = true;
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
ProcStartInfo.Arguments = "/c start /wait batch.bat ";
MyProcess.StartInfo = ProcStartInfo;
MyProcess.Start();
MyProcess.WaitForExit();
I added the /wait so your c# code is going to wait for your batch to finish, to pursuit the c# code execution
I'm trying to run CMD from my code. This is the line that I run in my command line, and it works when I run it manually:
C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
This is what I have in my code:
string commandText = String.Format("/C {0}{1} & node csvToJson.js", root, csvToJsonFolder);
Process.Start("CMD.exe", commandText);
commandText evaluates to /C C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js
It runs without error, but nothing seems to have happened. The command prompt doesn't open, so I can't see any errors that may have occurred. The command is supposed to result in a file being written to a particular folder, and when I run the command manually the file gets written, but when I run my code the file does not get written.
EDIT: I changed my code to this:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = commandText;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
string result = process.StandardOutput.ReadToEnd();
The result is just an empty string. There is no error message or anything.
The problem was in my command text, I forgot "cd". Should have been "/C cd C:\Dev\MySite\web\Website\comparison-tool\data\ & node csvToJson.js"
Previously somebody has asked how to run a command line command in C# from visual studio and the beneath was the answer.
I tried the same intended to call a tool called cccc which can run on command line. But when I run the beneath code I do not get any results and nothing shows wrong.
Stating generally how can we run the same commands from C# as it was in command line and get the same results. Say I call a program (it could be any program that is able to run on command line, for instance, cccc, ccm, and so on) on command line and get some results. How to call the command line and give the arguments so it will call in its turn the cccc or whatever and do the same thing as it was without C#.
string strCmdText;
strCmdText = "/C d: cd D:\\Exercises\\npp52\\PowerEditor\\src && dir /s /b | cccc - --outdir=d:\\myfolder";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Add 'pause' to the end of your command:
string strCmdText;
strCmdText = "/C d: cd D:\\Exercises\\npp52\\PowerEditor\\src && dir /s /b | cccc - --outdir=d:\\myfolder & pause";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
or redirect console standard output to a stream.
Here need more magic with OutputDataReceived handler
void Main()
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName="cmd.exe";
proc.StartInfo.Arguments = "/c ping 127.0.0.1";
proc.StartInfo.UseShellExecute = false;
proc.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();
proc.Close();
}
private void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
// Do what You need with out
Console.WriteLine(outLine.Data);
}
}
Instead of trying to put everything inside a string you could take advantage of the ProcessStartInfo class to better define your arguments
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.WorkingDirectory = "D:\\Exercises\\npp52\\PowerEditor\\src ";
psi.Arguments = "/C dir /s /b | cccc - --outdir=d:\\myfolder"";
psi.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(psi);
Also with the command window open you could see if there are syntax errors in your command
Another problem is that you are not using RedirectStandardOutput, so output is discarded. Take a look at this answer.