Coder Perfect

Commands to Execute from the Command Prompt


Is it possible to execute command prompt commands from a C# application? If that’s the case, how would I go about doing the following:

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

This essentially embeds a RAR file inside a JPG image. I was simply curious if there was a method to automate this in C#.

Asked by user

Solution #1

That’s all there is to it. use C# to execute shell commands

string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";


The cmd window will be hidden as a result of this.

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;


It’s crucial that the argument starts with /C, or else it won’t function. “It carries out the command supplied by the string and then terminates,” Scott Ferguson explained.

Answered by RameshVel

Solution #2

I tried RameshVel’s solution, but my console application couldn’t give parameters. If anyone else is having the same issue, here’s how to fix it:

using System.Diagnostics;

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;

cmd.StandardInput.WriteLine("echo Oscar");

Answered by Ogglas

Solution #3

var proc1 = new ProcessStartInfo();
string anyCommand; 
proc1.UseShellExecute = true;

proc1.WorkingDirectory = @"C:\Windows\System32";

proc1.FileName = @"C:\Windows\System32\cmd.exe";
proc1.Verb = "runas";
proc1.Arguments = "/c "+anyCommand;
proc1.WindowStyle = ProcessWindowStyle.Hidden;

Answered by HackerMan

Solution #4

For some reason, none of the following responses seemed to assist; they seemed to push issues under the rug and make troubleshooting one’s command harder. So I came up with something like this, which might be useful to someone else:

var proc = new Process
    StartInfo = new ProcessStartInfo
        FileName = @"C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE\tf.exe",
        Arguments = "checkout AndroidManifest.xml",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        WorkingDirectory = @"C:\MyAndroidApp\"


Answered by Matt

Solution #5

Though technically this doesn’t directly answer question posed, it does answer the question of how to do what the original poster wanted to do: combine files. This is mostly a post to assist newcomers in comprehending what Instance Hunter and Konstantin are discussing.

This is the approach I take when combining files (in this case a jpg and a zip). Note that I build a buffer that is filled with the zip file’s content (in little pieces rather than in one massive read operation), and then the buffer is written to the back of the jpeg file until the zip file’s end is reached:

private void CombineFiles(string jpgFileName, string zipFileName)
    using (Stream original = new FileStream(jpgFileName, FileMode.Append))
        using (Stream extra = new FileStream(zipFileName, FileMode.Open, FileAccess.Read))
            var buffer = new byte[32 * 1024];

            int blockSize;
            while ((blockSize = extra.Read(buffer, 0, buffer.Length)) > 0)
                original.Write(buffer, 0, blockSize);

Answered by CarllDev

Post is based on