:)
I have a software which can be executed via command line, and now I want it to be executed directly from my C# app. Sadly, there is no error but I still can't do it. :(
The path of .exe file of the software is C:\program files\mysoftware.exe
The command I would like to input is
cd c:\program files\mysoftwareFolder
enter
mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand
enter
exit
The commands above work so well in the actual command prompt, but they just don't work from my C# code.
Here is the code:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine(#"cd" +#"program files\mysoftwarefolder");
sw.WriteLine(#"mysoftware.exe" +#"d:\myfolder\file1.xxx" +#"d:\myfolder\file2.xxx" +#"-mycommand");
sw.WriteLine(#"exit");
sw.Close();
sr.Close();
I guess the incorrect parts might be "startinfo.FileName = "cmd";" or the way I typed the command in the code, but I have no idea how to correct them. :(
Please tell me what I did wrong. I appreciate every answer from you! :)))
UPDATE Thank you for your helps! I tried writing the command in batch file, but it only works in debugging mode. (I forgot to tell you guys that I am developing a web service.) When I run my external project which will use this C# service, it won't work. I don't know whether I should add something to my code or not.
help meeeeee pleaseeeee (T___T)
Write these commands in a batch file and execute the batch file.
In batch file:
cd c:\program files\mysoftwareFolder
mysoftware.exe
d:\myfolder\file1.xxx
d:\myfolder\file2.xxx -mycommand
exit
Code:
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "path to batchfile.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
Instead of:
startinfo.FileName = "cmd";
Directly use
startinfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
Then pass the arguments to the start info as
startinfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
So the whole code looks like:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
p.StartInfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
If you need to see output from your program you can simply use the output string.
2 things: I think you have spacing problems and you're not reading the result of these commands. cmd is probably telling you ..."is not recognized as an internal or external command"
If you look at what you're throwing at cmd, it will be:
echo on
c:
cdprogram files\mysoftware folder
mysoftware.exed:\myfolder\file1.xxx
That won't work when you try it in cmd. CMD is almost certainly kicking back error messages at you, but you're never reading from sr so you'll never know it.
I'd add in some spaces and include all the paths in quotes internally like so:
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine("cd \"program files\\mysoftwarefolder\"");
sw.WriteLine("mysoftware.exe \"d:\\myfolder\\file1.xxx\" d:\\myfolder\\file2.xxx\" -mycommand");
sw.WriteLine(#"exit");
Related
First of all, i searched a lot to avoid asking a duplicate question. If there is one, i will delete this question immediately.
All the solutions on the web are suggesting to use Process.StartInfo like this one
How To: Execute command line in C#, get STD OUT results
I dont want to run a batch file, or an .exe.
I just want to run some commands on cmd like
msg /server:192.168.2.1 console "foo" or ping 192.168.2.1
and return the result if there is one.
How can i do that ?
Those commands are still exe files, you just need to know where they are. For example:
c:\windows\system32\msg.exe /server:192.168.2.1 console "foo"
c:\windows\system32\ping.exe 192.168.2.1
The only proper way to do this is to use Process.Start. This is demonstrated adequately in this question, which is itself a duplicate of two others.
However, as DavidG says, the commands are all exe files and you can run them as such.
Apparently, i found an answer
while (true)
{
Console.WriteLine("Komut giriniz.");
string komut = Console.ReadLine();
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C" + komut;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
Console.WriteLine(process.Start());
string line = "";
while (!process.StandardOutput.EndOfStream)
{
line = line + System.Environment.NewLine + process.StandardOutput.ReadLine();
// do something with line
}
Console.WriteLine(line);
Console.ReadLine();
}
seems like if you can run cmd.exe with arguments including your command.
thanks for contributing.
I am working on a project of remotely receiving commands from a server, but I am facing a problem when working with the command prompt locally. Once I get it working locally, then I will move to remote communication.
Problem:
I have to completely hide the console, and client must not see any response when the client is working with the command line but it will show a console for a instance and then hide it.
I had to use c# to send a command to cmd.exe and receive the result back in C#. I have done it in one way by setting the StandardOutput... and input to true.
The commands are not working. For example, D: should change the directory to D and it does, but after that, if we use dir to see the directories in D, it does not show the appropriate directories.
Here is my code:
First Method
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + textBoxInputCommand.Text + " >> " + " system";
process.StartInfo = startInfo;
process.Start();
Second Method
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + textBoxInputCommand.Text);
procStartInfo.WorkingDirectory = #"c:\";
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = true;
procStartInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
richTextBoxCommandOutput.Text += result;
I want the program to run as administrator because the exe it generates does not run commands when it runs from the C drive.
Try not to run the commands by passing them to cmd instead write the commands passed by the client to a.bat file execute the .bat. file from your program this will probably hide your command prompt window.
You can also use process.OutputDataRecieved event handler to do anything with the output.
If you want to execute command using administrator rights you can use runascommand. It is equivalent to the sudo command in Linux. Here is a piece of code may be it will help you
var process = new Process();
var startinfo = new ProcessStartInfo(#"c:\users\Shashwat\Desktop\test.bat");
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
process.StartInfo = startinfo;
process.OutputDataRecieved += DoSomething;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
//Event Handler
public void DoSomething(object sener, DataReceivedEventArgs args)
{
//Do something
}
Hope it helps you.
You could hide command prompt window by adding this line of code:
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
or do not create it at all
startInfo.CreateNoWindow = true;
Here can be found a few awarding solutions:
Run Command Prompt Commands
I want to run an exe file by my c# code. The exe file is a console application written in c#.
The console application performs some actions which includes writing content in database and writing some files to directory.
The console application (exe file) expects some inputs from user.
Like it first asks , 'Do you want to reset database ?' y for yes and n for no.
again if user makes a choice then application again asks , 'do you want to reset files ?'
y for yes and n for no.
If user makes some choice the console application starts to get executed.
Now I want to run this exe console application by my c# code. I am trying like this
string strExePath = "exe path";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = strExePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
I want to know how can I provide user inputs to the console application by my c# code?
Please help me out in this. Thanks in advance.
You can redirect input and output streams from your exe file.
See redirectstandardoutput
and redirectstandardinput for examples.
For reading:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
For writing:
...
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.WriteLine("y");
...
myStreamWriter.Close();
ProcessStartInfo has a constructor that you can pass arguments to:
public ProcessStartInfo(string fileName, string arguments);
Alternatively, you can set it on it's property:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "some argument";
Here is a sample of how to pass arguments to the *.exe file:
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = #"\filepath.exe";
p.StartInfo.Arguments = "{insert arguments here}";
p.Start();
error += (p.StandardError.ReadToEnd());
p.WaitForExit();
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();
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);