How to run a .exe from c# by passing arguments? - c#

I had tried this code but not sure if this is correct. The .exe file getting opened(partially) but not performing the action.
Please help me.
Code :
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardError = true;
startInfo.WorkingDirectory = #"C:\\Program Files\\SQL Sentry\\SQL Sentry Plan Explorer";
startInfo.FileName = #"C:\\Program Files\\SQL Sentry\\SQL Sentry Plan Explorer\\SQL Sentry Plan Explorer.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = #"C:\\Program Files\\SQL Sentry\\SQL Sentry Plan Explorer\\XMLFile1.xml";
System.Threading.Thread.Sleep(1000);
try
{
using (Process p = Process.Start(startInfo))
{
p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
}
catch (Exception e17)
{
MessageBox.Show("17");
MessageBox.Show(e17.ToString());
}

Remove the double slash from your strings and place single quotes at the argument
startInfo.WorkingDirectory = #"C:\Program Files\SQL Sentry\SQL Sentry Plan Explorer";
startInfo.FileName = #"C:\Program Files\SQL Sentry\SQL Sentry Plan Explorer\SQL Sentry Plan Explorer.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = #"'C:\Program Files\SQL Sentry\SQL Sentry Plan Explorer\XMLFile1.xml'";

Try wrapping the path to the file (argument) in single quotes:
startInfo.Arguments = #"'C:\\Program Files\\SQL Sentry\\SQL Sentry Plan Explorer\\XMLFile1.xml'";
You can always try to run the exe manually from command line to confirm that it works as expected.

Simply use,
System.Diagnostics.Process.Start(#"C:\Windows\system32\notepad.exe", "[arguments]");
more # Process

Related

How to give 2nd input to a '.exe' file after the Process.start method is called?

I have to execute a '.exe' file which produces a output and asks for another input again. I am able to run the first part but I am not able to pass the second parameter to complete the process.
Here is my code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Context.exe";
startInfo.Arguments = "xyz";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process.Start(startInfo);
I would use StandardInput in conjunction with RedirectStandardInput. You can pass any data that a user would enter on the command line using this StandardInput StreamWriter object. If this application has a user interface, you may need to do something else entirely.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Context.exe";
startInfo.Arguments = "xyz";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardInput = true;
var p = Process.Start(startInfo);
// Write whatever data you need to send to the application here.
p.StandardInput.Write("y");
You can use startInfo.RedirectStandardInput. See here for more details

bash pipes - I am trying to call script from c#

I have a cygwin bash scripts that works:
#!/bin/sh
cd myc
cp Stats.txt Stats.txt.cpy;
cat Stats.txt.cpy | sort -n -k1 | gawk '{sum+=$2; print $0,sum}' > Stats.txt
I want to "call" it from C#:
string cmd="myscript.sh";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\Cygwin\bin\bash.exe");
psi.Arguments = cmd;
psi.WorkingDirectory = "C:\\cygwin\\home\\Moon";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
proc.StartInfo = psi;
proc.Start();
string error = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
this.textBox1.AppendText(error);
this.textBox1.AppendText(output);
It works fine from cygwin terminal BUT from C# I get:
Input file specified two times.
I suspect this is a pipes thing - can anyone help?
It was a path issue.
You need to set path in the script - otherwise it uses a different "non-cygwin" path and gets wrong commands.

How to run CL.exe using Process.Start()?

I have following code
using (StreamWriter outfile = new StreamWriter(#"f:\trial.cpp"))
{
outfile.Write(txtCode.InnerText);
}
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(#"cl.exe", #" 'trial.cpp'");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.UserName = "asdasd";
SecureString secureString = new SecureString();
foreach (char c in "abcded")
{
secureString.AppendChar(c);
}
procStartInfo.Password = secureString;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = #"f:\";
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
How to pass file name as parameter? Above code doesn't run and I have tried all full path, different path options.
can anyone help?
The argument is set incorrectly. You have:
var procStartInfo = new ProcessStartInfo(#"cl.exe", #" 'trial.cpp'");
Where there are spaces and single quotes in the name. Try:
var procStartInfo = new ProcessStartInfo(#"cl.exe", #"trial.cpp");
EDIT:
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "CL.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "trial.cpp";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// error handling
}
The point here is that CL is a command line executable, not a windows GUI application.
http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx
http://msdn.microsoft.com/en-us/library/kezkeayy.aspx
http://msdn.microsoft.com/en-us/library/9s7c9wdw.aspx
If the cl.exe is not in the system PATH (which by default it is not) then the start process will not find the executable and it will fail to run.
So I suspect you are seeing the fact that the cl.exe is not in the system PATH.

Execute command line using C#

All I am trying to do is send a command that opens a model with the program.exe
Supposed to be super simple!
Ex:
"C:\Program Files (x86)\River Logic\Enterprise Optimizer 7.4 Developer\EO74.exe" "C:\PauloXLS\Constraint Sets_1.cor"
The line above works well if pasted on the command prompt window.
However, when trying to pass the same exact string on my code it gets stuck on C:\Program
string EXE = "\"" + #tbx_base_exe.Text.Trim() + "\"";
string Model = "\"" + #mdl_path.Trim()+ "\"";
string ExeModel = EXE + " " + Model;
MessageBox.Show(ExeModel);
ExecuteCommand(ExeModel);
ExeModel is showing te following line on Visual Studio:
"\"C:\\Program Files (x86)\\River Logic\\Enterprise Optimizer 7.4 Developer\\EO74.exe\" \"C:\\PauloXLS\\Constraint Sets_1.cor\""
To me looks like it is the string I need to send in to the following method:
public int ExecuteCommand(string Command)
{
int ExitCode;
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Process.Close();
return ExitCode;
}
Things I've tried:
Pass only one command at a time (works as expected), but not an option since the model file will open with another version of the software.
Tried to Trim
Tried with # with \"
Can anyone see any obvious mistake? Thanks.
It's pretty straightforward. You just create a command line object then write to it, then to execute it you read back from it using SR.ReadToEnd():
private string GETCMD()
{
string tempGETCMD = null;
Process CMDprocess = new Process();
System.Diagnostics.ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo();
StartInfo.FileName = "cmd"; //starts cmd window
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.CreateNoWindow = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //required to redirect
CMDprocess.StartInfo = StartInfo;
CMDprocess.Start();
System.IO.StreamReader SR = CMDprocess.StandardOutput;
System.IO.StreamWriter SW = CMDprocess.StandardInput;
SW.WriteLine("#echo on");
SW.WriteLine("cd\\"); //the command you wish to run.....
SW.WriteLine("cd C:\\Program Files");
//insert your other commands here
SW.WriteLine("exit"); //exits command prompt window
tempGETCMD = SR.ReadToEnd(); //returns results of the command window
SW.Close();
SR.Close();
return tempGETCMD;
}
Why are you opening a command prompt (cmd.exe)? Just pass the name of the executable.

What is wrong in how I start the process?

When I run the following from cmd it runs ok
>mysql -h 134.86.157.132 -u sas vm < c:\vm.sql
When I try to do the same from code it does not work
ProcessStartInfo info = new ProcessStartInfo("mysql");
info.Arguments = #"-h 134.86.157.132 -u sas vm < c:\vm.sql";
info.Domain = "134.86.157.132";
info.UserName = "sas";
info.Arguments = #"vm < c:\vm.sql";
info.UseShellExecute = false;
Process.Start(info);
What am I doing wrong here? It does not work and I get some wrong password exception?
Edit:
I run it like this now
ProcessStartInfo info = new ProcessStartInfo("mysql");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.Arguments = #"-h 134.86.157.132 -u sas vm < c:\vm.sql";
Process.Start(info);
and get following error The system cannot find the file specified
"-h 134.86.157.132 -u sas vm" are the arguments, don't use domain and username.
You also need to redirect the standard input stream to pass your vm.sql file in. See this example: ProcessStartInfo.RedirectStandardInput Property
Try it like this:
ProcessStartInfo info = new ProcessStartInfo("mysql");
info.Arguments = #"-h 134.86.157.132 -u sas vm < c:\vm.sql";
info.UseShellExecute = false;
Process.Start(info);
You're setting UseShellExecute to false, but it's the shell that interprets the "<" part to redirect stdin from a file.
Unless there's a specific reason why you need UseShellExecute to be false, set it to true. Alternatively, redirect standard input for the new process, and give it the data directly.
EDIT: As specified in other answers, keep the original arguments, and don't set Domain/UserName on the ProcessStartInfo. Those refer to Windows usernames and domains, not MySQL ones.
At that point you'll be able to set UseShellExecute to true with no ill effects, with any luck.
How about something like from this post:
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 = new StreamWriter(p.StandardInput))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -h 134.86.157.132 -u sas vm < c:\vm.sql");
}
}

Categories

Resources