Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Command Prompt Commands

Is there any way to run command prompt commands from within a C# application? If so how would I do the following:

copy /b Image1.jpg + Archive.rar Image2.jpg 

This basically embeds an RAR file within JPG image. I was just wondering if there was a way to do this automatically in C#.

like image 762
user Avatar asked Sep 24 '09 04:09

user


People also ask

What is the Run command in cmd?

The Run command on an operating system such as Microsoft Windows and Unix-like systems is used to directly open an application or document whose path is known.

How do you do a Run command?

Use the shortcut keys for Run: Windows + R Simply hold down the Windows key and press R on your keyboard.


1 Answers

this is all you have to do run shell commands from C#

string strCmdText; strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; System.Diagnostics.Process.Start("CMD.exe",strCmdText); 

EDIT:

This is to hide the cmd window.

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 copy /b Image1.jpg + Archive.rar Image2.jpg"; process.StartInfo = startInfo; process.Start(); 

EDIT: 2

Important is that the argument begins with /C otherwise it won't work. How Scott Ferguson said: it "Carries out the command specified by the string and then terminates."

like image 157
RameshVel Avatar answered Sep 18 '22 09:09

RameshVel