I have been toying with the Process class in C#. I have some code below I'd like to use to open cmd.exe and run the DIR command. However, when I attempt to use the code, the cmd.exe opens, but no command is run. Why is this happening and how do I fix it?
Process cmd = new Process();
cmd.StartInfo.FileName = #"cmd.exe";
cmd.StartInfo.Arguments = #"DIR";
cmd.Start();
cmd.WaitForExit();
Try passing the /K option to let the command console stay at video and receive the subsequent DIR command (Without exit).
Process cmd = new Process();
cmd.StartInfo.FileName = #"cmd.exe";
cmd.StartInfo.Arguments = #"/K DIR"; // <-- This will execute the command and wait to close
cmd.Start();
cmd.WaitForExit();
The /K option will allow you to get a better understanding of what happens in the command window because the window will not close immediately and you need to click the close button or type the Exit command. If you want to exit after issuing your commands then use the /C option.
cmd.StartInfo.Arguments = #"/c DIR";
Related
Below is the code where I'm trying to run one command with argument. (Call Tectia SFTP client profile & upload file)
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = $"/c sftpg3 {profile}";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
using (StreamWriter sw = cmd.StandardInput){
if (sw.BaseStream.CanWrite)
sw.WriteLine($"/c sput {filename} {output}");
}
After the process started, it logins into the into the SFTP and stucked. It won't input the next command as it deemed as another program.
Would like to ask how does it execute the next command after login? I tried Calling CMD with && concatenating and it won't works too. We can only use SFTP via command line as client requested.
Launch sftpg3 with the -B - option to read from standard input.
Launch sftpg3 with the -B <filename> option to read from a batch
file of commands.
More details of command line arguments is available in the documentation.
Also, I don't think you want to write /c the second time around. /c is just something passed to cmd.exe. On that note, why are you calling cmd.exe instead of the binary directly?
I am working on developing the visual studio plugin. As a part of it, I want to execute one bash file. This bash file opens the command prompt.
Once command prompt is open , we want to write / executes bunch of commands on it.
I have tried it like this:
System.Diagnostics.Process process = System.Diagnostics.Process.Start("MyBash.bat");
process.WaitForExit(5000);
process.StandardInput.WriteLine("echo %PATH%");
But I can see that, it open the command prompt but fail to write command on it.
It throws the exception at the line of writing the command to it. It seems like the command prompt open from this bash file has different process id.
Please help me to resolve it.
Try to execute directly in your batch file:
Insert this to your batch:
start cmd.exe /k "echo %PATH%"
You can also use
/c
if you want to close the cmd After Execution
EDIT:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.FileName = "mybash.bat";
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}
I have a c# app (app.exe), which I want to run from a command line window and then to CLOSE the command line window after the app started.
I tried to search for "cmd" in the processes list and to close it (cmdProcess.CloseMainWindow()) but in this case, if I run app.exe by double-click only, and there is another cmd opened separately, it will be closed- and I don't want that.
How can I know which cmd instance runs my app?
thanks
In Windows Command prompt it's "&" to attach commands in one line. So it'll be:
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/k " + Command + " & exit";
But if you read the "cmd /?", you'll see that the purpose of "/k" argument is to keep the window. So if it's not what you want, just use the "/c" argument instead.
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + Command;
try this
Process process = new Process();
process.StartInfo.Arguments = Command + "; exit";
the ";" is to separate between the commands and the "exit" is the next command to execute
I wanted to use my c# visual studio to open command prompt using process.StartInfo.FileName and process.StartInfo.Arguments to give commands automatically however this does not work out. Anyone knows how to do this? ( I am using the cmd to call for my python script )
Try using the following. Hope this helps. Replace notepad.exe with something else you like.
Process p = new Process();
p.StartInfo = new ProcessStartInfo("cmd.exe", "/c notepad.exe");
p.Start();
Please refer to http://ss64.com/nt/cmd.html for more details.
This opens notepad and a file called foo
var procInfo = new ProcessStartInfo("notepad");
procInfo.Arguments = "foo.txt";
var proc = new Process();
proc.StartInfo = procInfo;
proc.Start(); //Actually executes the process
proc.WaitForExit(); //Waits until the process completes, in this case, when you close notepad
So for your thing you would call the python interpreter and pass the python script as arguments, along with anything else the interpreter needs (I have 0.1% knowledge of python)
Important note: Make sure your executable is in the path.
I have to compile a game like this
love "C:\testgame"
in the cmd. So I use this code, but it seems like the parameter is missinterpreted. Also, the console closes after a sec. But if I use Messagebox.Show I can see the command in the cmd is the same I manually use (and this works)
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput =
true;
cmd.StartInfo.RedirectStandardOutput =
true;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.Write(#"cd %ProgramFiles(x86)%\LOVE\");
MessageBox.Show("love \""+fldBrowDiag.SelectedPath.ToString()+#"\"+lsb_projects.SelectedItem.ToString()+"\"");
cmd.StandardInput.Close();
cmd.Close();
First, the "cd" command you issue will probably fail because you don't have quotes around the argument. (that program files env variable will have spaces in it.)
Second, instead of writing to stdin directly, maybe consider using the "/c" switch that will instruct cmd.exe to execute the specified commands directly. You can separate the commands with '&&'.
Try this to simplify things:
var process = Process.Start(
new ProcessStartInfo(#"C:\Program Files (x86)\LOVE\love.exe", #"C:\game") {
WorkingDirectory = #"C:\Program Files (x86)\LOVE" });
Why can't you just start cmd with the correct arguments to launch your process?
eg cmd /C love "c:\game" to close after finish or cmd /K love "c:\game to leave open after finish?