execute batch command in c# - c#

I would like to run a batch command in c# code.
I tried this code but it only pops up cmd but I want it to pop up cmd and displays "Hello World".
Process.Start("cmd", "echo Hello World");
Is it possible to do this?

Try this:
Process.Start("cmd", "/K echo Hello World");
(Note the "/K")
based on this reference for cmd.exe

Related

Use CMD commands in C# For showing Tasklist

I need to find the PID number for a process using C# and tasklist in the CMD.
the PID number needs to be put into a textbox in a c# form.
The code for finding the pid number in Command prompt is this.
for /f "tokens=1,2" %a in (' Tasklist /fi "imagename eq notepad.exe" /nh') do #echo %b
But I don´t know how to integrate CMD commands into C# winform.
You can achieve the same thing in .NET by using Process.GetProcessesByName and then outputting the process Id:
foreach (var p in Process.GetProcessesByName("notepad"))
{
Console.WriteLine(p.Id);
}
Alternatively, if you really want to use the cmd window and capture the output, you can create a process that will run cmd.exe and pass it the command line you want to execute (add a /C at the beginning, which tells cmd.exe to close the cmd window after running). You also want to RedirectStandardOutput, which allows you to capture the output of the command that was run. Then you can use proc.StandardOutput.ReadLine() to get each line that was returned:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C for /f \"tokens=1,2\" %a in " +
"('Tasklist /fi \"imagename eq notepad.exe\" /nh') do #echo %b",
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
proc.Start();
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
{
Console.WriteLine(proc.StandardOutput.ReadLine());
}
You can certainly initiate a command prompt window ( Console ) and capture its stdout and stderr streams to grab the data and then parse it and show it in a windows forms control,
at the same time, usually in a case like this the Process class part of standard .NET Framework is used to retrieve any information of a running process on thw Windows machine,
have a look at this methos in MSDN for example: Process.GetProcessesByName or any other method of the Process class, that way you can do it without running any command in any console window.

How to execute multiple commands - C#

There are 2 PCs(server & node). The Selenium hub is up & running. The notifications are seen in its cmd window.
Now, I'm trying to set up another PC as a Selenium node. To do that I need to run 2 commands from the server PC command prompt.It works when done manually.Failing to do so programatically.
Here is what I have so far.
private static void StartSeleniumNode()
{
string Command1 = "/C cmdkey.exe /add:ABCDES181 /user:abc /pass:abc#123 & ";
string Command2 = "psexec.exe \\ABCDES181 -i -w D:\\Selenium java -jar selenium-server-standalone-2.47.1.jar -role node -hub http://someip:4444/grid/register";
Process.Start(cmd.exe, Command1 + Command2);
}
When run, a cmd window just pops up and closes. There would be a notification if a node is registered, but nothing of that sort here. I think it is the syntax to run 2 commands that is the issue here.
The way to tell cmd to run multiple commands is to chain them using &&.
For ex, you could get your command prompt to do this:
echo hello && echo world
In your case, try using this statement:
Process.Start(Constants.CommandPrompt, string.Format("{0} && {1}", Command1,Command2));

How do I run a Python script from command line in a WPF application

I am trying to run a Python script from cmd line in a WPF application. I have this code which opens cmd line to the correct directory:
string cmd = #"webplus_builder.py";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"C:\Temp\WebBuilder";
p.StartInfo.Arguments = string.Format("{0} {1} {2}", cmd, "WAF", _CountryCode);
p.StartInfo.UseShellExecute = false;
p.Start();
I now want to run webplus_builder.py from that directory like so:
How do I achieve this? Is it a case of just adding arguments to my Process as I've attempted to do?
You cannot execute python scripts directly, it's python itself that has to be run: Use the name of your script as the first parameter to it, the following parameters are passed to your script.
(Windows handles a double click on py-files by "opening them with" python - exactly as described).
Read the official docs for details:
1. Command line and environment — Python 2.7.9 documentation
1. Command line and environment — Python 3.3.6 documentation

C# Process.Start: Whitespace Not Working Even Quoted

What I got:
Process.Start("cmd.exe", "/K \"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"");
Even though I wrapped the filename with escaped ", it still displays the error:
'C:/Program' is not recognized as an internal or external command, operable program or batch file.
What is wrong?
You need to use two " for spaces in program path:
Process.Start("cmd.exe", "/K \"\"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"\"");
You code will be translated to
cmd.exe /K "C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js"
cmd.exe will translate it to
C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js
That's why it complain errors.
What you need is to enclose whole node.exe command with double quote again.
Process.Start("cmd.exe", "/K \"\"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"\""); so the node.exe command will be "C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js"
BTW, why don't just call node.exe directly?
Process.Start("C:/Program Files/nodejs/node.exe", "C:/rc/rainingchain/app.js");

Sending arguments to the command line

I need to unzip a compressed file with the command line version of 7zip. This one liner should to the trick:
Process.Start("cmd", #"C:\Users\cw\Downloads\7za920\7za e C:\UPDATED.zip -oc:\");
I'm specifying the path to the 7zip command line executable, and telling it which file to unzip. If I copy and paste those arguments into my command line window, it will work. In C#, it will bring up a command line window, and nothing will happen. What gives?
Try:
Process.Start("cmd", #"/c C:\Users\cw\Downloads\7za920\7za e C:\UPDATED.zip -oc:\");
It's because you're running cmd.exe, and not 7za directly. You can do either of the two:
Process.Start(#"C:\users\...\7za", "e c:\updated.zip -oc:\");
or
Process.Start("cmd", #"/c c:\users\...\7za e c:\updated.zip -oc:\");
The /c flag tells cmd to run the arguments after starting.
Try
Process.Start(#"C:\Users\cw\Downloads\7za920\7za.exe", #"e C:\UPDATED.zip -oc:\");

Categories

Resources