C# Call EXE from console app - c#

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........................................."

Related

Run command process as Admin

I have a Windows Forms Application with the app.manifest set to:
level="requireAdministrator" uiAccess="false".
Now I need to run a console application (Diskspd.exe) from my application and return the standard output. It almost works, though the part that requires runAs Admin of DiskSpd fails.
Here is the part of the code that call the process (Example of txtDatPath.Text is "C:\temp\IO.dat"):
if (File.Exists(txtDatPath.Text))
{
File.Delete(txtDatPath.Text);
}
string qPath = string.Format("\"{0}\"", txtDatPath.Text);
if (rdoOLTP.Checked)
{
DScmd = "-b8K –d180 -h -L –o32 –t3 -r –w75 -c5G " + qPath;
}
else
{
DScmd = "–b60K –d60 -h -L –o32 –t1 -s –w100 –c1G " + qPath;
}
//now set up the cmd
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Verb = "runas";
p.StartInfo.Arguments = DScmd;
p.StartInfo.FileName = "diskspd.exe";
p.Start();
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I need the process itself to run as Admin to succeed. The command behaves the same way if I run regular command then try CMD and run as admin it completes.
StartInfo.UseShellExecute needs to be set to false for the RedirectStandardOutput to work.
Well - after all of this - in adding debugging, the issue is the p.StartInfo.Arguments = DScmd;
The Argument is being interpreted as separate entries.
The solution/problem: DISKSPD requires ASCII and the string is UNICODE.
Changed the hyphens to convert.ToChar(45)
So I am going to close this case (thx all!) and open new one.

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

How to show console when running a process?

I'm trying to launch a command in a console window / I'm using a gtk form/
So I've tried to launch it this way:
Process p = new Process ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "bash";
p.StartInfo.CreateNoWindow = false;
p.StartInfo.Arguments ="/tmp/test.sh";
p.Start ();
p.WaitForExit ();
but it won't show any thing.
for those who only use windows, it's something like:
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments =" c:\\test.bat";
I've tried to change UseShellExecute to true but the problem still exist..
Any Ideas??
Bash runs the script but if you want to see output, you need to run it in a terminal.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "gnome-terminal"; // Replace with whichever terminal you want to use
p.StartInfo.CreateNoWindow = false;
p.StartInfo.Arguments ="-x bash /tmp/test.sh";
//p.StartInfo.Arguments ="-e \"bash -c /tmp/test.sh;bash\""; // Use this if you want the terminal window to stay open
p.Start();
p.WaitForExit();

Is this possible to run a python code from C# through command prompt?

I want to run python code from C# through command Prompt.The Code is attached below
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"d:";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"cd D:\python-source\mypgms");
p.StandardInput.WriteLine(#"main.py -i example-8.xml -o output-8.xml");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);
Output :
D:\python-source\mypgms>main.py -i example-8.xml -o output-8.xml
D:\python-source\mypgms>
But nothing happened.Actually main.py is my main program and it takes 2 arguments. one is input xml file and another one is converted output xml file.
But i dont know how to run this python script from C# through command prompt. Please Guide me to get out of this issue...
Thanks & Regards,
P.SARAVANAN
I think you are mistaken in executing cmd.exe. I'd say you should be executing python.exe, or perhaps executing main.py with UseShellExecute set to true.
At the moment, your code blocks at p.WaitForExit() because cmd.exe is waiting for your input. You would need to type exit to make cmd.exe terminate. You could add this to your code:
p.StandardInput.WriteLine(#"exit");
But I would just cut out cmd.exe altogether and call python.exe directly. So far as I can see, cmd.exe is just adding extra complexity for absolutely no benefit.
I think you need something along these lines:
var p = new Process();
p.StartInfo.FileName = #"Python.exe";
p.StartInfo.Arguments = "main.py input.xml output.xml";
p.StartInfo.WorkingDirectory = #"D:\python-source \mypgms";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
Also the Python script appears to output to a file rather than to stdout. So when you do p.StandardOutput.ReadToEnd() there will be nothing there.
Why not host IronPython in your app and then execute the script?
http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx
http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application
or use py2exe to pragmatically convert your python script to exe program.
detail steps...
download and install py2exe.
put your main.py input.xml and output.xml in c:\temp\
create setup.py and put it in folder above too
setup.py should contain...
from distutils.core import setup
import py2exe
setup(console=['main.py'])
your c# code then can be...
var proc = new Process();
proc.StartInfo.FileName = #"Python.exe";
proc.StartInfo.Arguments = #"setup.py py2exe";
proc.StartInfo.WorkingDirectory = #"C:\temp\";
proc.Start();
proc.WaitForExit();
proc.StartInfo.FileName = #"C:\temp\dist\main.exe";
proc.StartInfo.Arguments = "input.xml output.xml";
proc.Start();
proc.WaitForExit();

How to launch an external process in C#, using a cmd window

I am having trouble executing an external console application using Process.Start
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "test.exe";
p.StartInfo.Arguments = s;
p.Start();
When the argument that p generates executes, the external application crashes, although if I copy the exact same argument in a command line window it runs fine.
So my question instead how would I create a new instance of a command window and then add the command test.exe + s to run?
So effectively I am launching cmd and then adding my arguments on to it
If you want to run test.exe prm1 prm2 via cmd, use cmd.exe /c test.exe prm1 prm2. Though I don't really understand what this has to do with the crashes. Sounds like your problem is with test.exe - find out what's causing it to crash, and that will help you fix your C# code so that you don't need the cmd.
One of the places I would examine is the working directory. When you set it to "dump", are you sure the current directory is what you expect? Try using a full path first. It's possible that test.exe happens to be in the system path so it gets executed, but its working directory is not what it expects, and this causes it to crash.
try this:
ProcessStartInfo processToRunInfo = new ProcessStartInfo();
processToRunInfo.Arguments = "Arguments");
processToRunInfo.CreateNoWindow = true;
processToRunInfo.WorkingDirectory = "C:\\yourDir\\";
processToRunInfo.FileName = "test.exe";
//processToRunInfo.CreateNoWindow = true;
//processToRunInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = processToRunInfo;
process.Start();
Process p = new Process();
p.StartInfo.WorkingDirectory = "/full/path/to/dump";
p.StartInfo.FileName = "/full/path/to/test.exe";
p.StartInfo.Arguments = s; // will call 'text.exe s'
p.Start();
Take a look at MSDN.
You need to Create an instance of the StartInfo class and user Start() such as
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.example.com";
Process.Start(startInfo);
Try it!
Rewriting your code would look something like this:
ProcessStartInfo startInfo = new ProcessStartInfo("test.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.WorkingDirectory = "dump";
startInfo.Arguments = "s";
Process.Start(startInfo);

Categories

Resources