Cannot see the results of command line - c#

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.

Related

How to execute ubuntu shell command in C# to get exitCode as 0

I am running Ubuntu Shell command through C# code, when I run this Script in Ubuntu it shows as 'Syntax OK' but when I run it through C# code it Always returns exitCode - 1. Please Correct my code if something id wrong.
string _exitCode="";
Process proc = new Process();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments ="-c \" apachectl -t -f /etc/apache2/sites-enabled/example.conf \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.BeginOutputReadLine();
proc.StandardError.ReadToEndAsync();
proc.WaitForExit(3000);
if (!proc.HasExited)
proc.Kill();
_exitCode = proc.ExitCode.ToString();
to correct command :
"-c \"/etc/init.d/apachectl -t -f /etc/apache2/sites-enabled/example.conf 2>&1\"";

executing .exe with argument and multiple commands from C#

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

C# Process removes quoted arguments

The command I'm trying to execute is
"C:\Program Files (x86)\MyAPP\solr-6.2.0\bin\solr" start -f -c -z "10.195.42.93:2181,10.195.42.92:2181" -h 10.195.42.92
And this works just fine on the command Line.
I'm trying to execute this as C# process.
NOTE: Below code works fine if i remove the quotes surrounding IPList
var IPList="10.195.42.93:2181,10.195.42.92:2181";
var hostIP="10.195.42.92"
string command = #"/c ""C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr"" start -f -c -z """ + IPList + #""" -h " + hostIP;
Process process = new Process();
log.Info("Starting " + command);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (s, e) => log.Info(e.Data);
process.ErrorDataReceived += (s, e) => log.Info(e.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
This throws error :
'C:\Program' is not recognized as an internal or external command,...
I have looked at the similar issue posted Here and tried the /s option but no heed.
What am i missing here ?
As the error says, it is trying to execute C:\Program which basically means something is wrong with the way you are escaping quotes. May be try to escape " using - \"
You're asking cmd.exe to run the command C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr. It will not run "C:\Program Files (x86)\MyApp\solr-6.2.0\bin\solr" because the quotes are consumed by the invocation of cmd.exe.
I do not know the proper way to fix this. You probably need to add escaped quotes somehow.
Enclosing the command with "(quotes) after /c solves the issue.
Example :
"/c ""c:\prog files\xyz\solr" start -c -z "blah,blah,blah" -h
IP "
Basically the command that is getting executed is
> cmd.exe /c "C:\Program Files (x86)\MyAPP\solr-6.2.0\bin\solr" start -f -c -z "10.195.42.93:2181,10.195.42.92:2181" -h 10.195.42.92
This command fails, and this has nothing to do with c# process api or .net.

adding arguments to process not working?

I got this program written in C# WinForms.
im using system.diagnostic to create a CMD process.
with that cmd i want some arguments but they are not present or working :S
dont know why ?!
NOTE: im not sure how to use more than 1 argument, correct me if im wrong :D
im trying to replicate the "copy /b %filename% lpt1" command....
here is my code:
public void OutputBtn_Process_Click(object sender, EventArgs e)
{
foreach (FileInfo fi in listBox1.Items)
{
Process process1 = new Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.Arguments = "copy /b myfile.txt test.txt";
//process1.StartInfo.LoadUserProfile = true;
process1.StartInfo.FileName = "cmd.exe";
process1.StartInfo.WorkingDirectory = Path.GetDirectoryName(fi.FullName);
process1.Start();
}
}
string strCmdText;
strCmdText= "/C copy /b myfile.txt test.txt";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Try this
process1.StartInfo.Arguments = "/C \"copy /b myfile.txt LPT1:\"";
The documentation on Windows 7 command-line tool cmd.exe

how to catch a standard output from my command line?

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

Categories

Resources