Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am beginner for C# and trying to develop an application that will...

1. open the command prompt on button click
2. Go to certain folder (where an XYZ.exe is copied)
3. execute some commands on the XYZ.exe.

I have searched on google and written some code that opens the command prompt but doesn't execute anything. I can see only blank command prompt.
Here is the code...

C#
private void btn_Merge_Click(object sender, EventArgs e)
        {
            ProcessStartInfo commandInfo = new ProcessStartInfo(); /// ("cmd.exe", "/K");
            commandInfo.WorkingDirectory = @"D:\TOOLS\" ; ///@"c:\";
            commandInfo.UseShellExecute = false;
            commandInfo.RedirectStandardInput = true;
            commandInfo.RedirectStandardOutput = true;
            commandInfo.FileName = "cmd.exe";
            commandInfo.Arguments = "srecord - 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex";
            
            Process process = Process.Start(commandInfo);
            
            command.Close();

        }




Please tell me where I am getting wrong.

What I have tried:

ied executing only commandInfo.Arguments = "/C ipconfig"; but even that didn't work.

I also tried, creating an instance of Process as...

C#
Process Command = new Process();

Command.start();
Command.StandardInput.WriteLine("srecord - 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex");
Console.WriteLine(command.StandardOutput.ReadToEnd());

// string result = command.StandardOutput.ReadToEnd();
// Display the command output.
// Console.WriteLine(result);
Posted
Updated 9-Aug-17 22:44pm
v2
Comments
Kornfeld Eliyahu Peter 9-Aug-17 8:46am    
Why should you open a command prompt (cmd.exe), go directly for your own executable...
PIEBALDconsult 9-Aug-17 9:17am    
Maybe you would like my
https://www.codeproject.com/Articles/70864/ProcessCommunicator

When you execute cmd.exe it runs a command prompt, it doesn't take the parameters as user input to the command box, it uses them as parameters to the cmd instruction.
And it's parameters mean it doesn't understand your arguments at all: CMD.exe (Command Shell) - Windows CMD - SS64.com[^]
You could do this - possibly - by using /C or /K followed by teh command you want to execute, but assuming srecord is the app you want to run, a better solution would be to execute that directly:
commandInfo.FileName = "srecord";
commandInfo.Arguments = "- 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex";

Process process = Process.Start(commandInfo);
 
Share this answer
 
I just tried something similar and it works fine. Try the following changes so you can actually see what is happening:
C#
commandInfo.RedirectStandardInput = false;
commandInfo.RedirectStandardOutput = false;
commandInfo.FileName = "cmd.exe";
commandInfo.Arguments = "/k srecord - 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex";
 
Share this answer
 
Comments
Psudonym 10-Aug-17 3:22am    
Yes... need to make it false. I was trying with true.
Basically, true works with console application. Mine is form application
I realised this after trying :) :) :)
Guys,

this worked...

private void ExecuteSrecCommand(String strCommand)
{
    ProcessStartInfo commandInfo = new ProcessStartInfo();
    commandInfo.WorkingDirectory = @"D:\\TOOLS\\srecord-1.63-win32";
    commandInfo.CreateNoWindow = true;
    commandInfo.UseShellExecute = false;
    commandInfo.RedirectStandardInput = false;
    commandInfo.RedirectStandardOutput = false;
    commandInfo.FileName = "cmd.exe";
    commandInfo.Arguments = strCommand;
    Process process = Process.Start(commandInfo);
    process.Close();
}


Now, there is another problem.

the above code opens the command prompt and executes the command.
But, the process.Close() doesn't closes/deletes the command prompt. In Task manager, I can see many instances of Command prompt are open.

I tried to close the commandInfo but commandInfo.Close doesn't exist :(

Please let me know, how to close the command prompt while exiting the function.
 
Share this answer
 
Comments
Nelek 10-Aug-17 4:42am    
Please don't use solutions to post more things about your problem. You can use the "edit" widget of your original question. Just be sure to make clear what you added, then people reading will answer or edit their existing solutions as well
Well,

even Command prompt can be closed.

I used

commandInfo.Arguments = "/C " + strCommand + " & exit"; 


and it is closing the command prompt after command execution.

If any other method is available, please share.
 
Share this answer
 
Comments
Nelek 10-Aug-17 4:43am    
Please don't use solutions to post more things about your problem. You can use the "edit" widget of your original question. Just be sure to make clear what you added, then people reading will answer or edit their existing solutions as well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900