Run command prompt command

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

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

This basically embeds a RAR file in the JPG image. I just want to know if there's a way to do this automatically in C.

#1 building

Yes, there are (see the link in Matt Hamilton's review), but it's easier and better to use IO classes with. NET. You can use File.ReadAllBytes to read the file, and then File.WriteAllBytes to write the embedded version.

#2 building

This is what you need to do. Run the shell command from C

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

Editor:

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();

Editor: 2

It is important that the parameter starts with / C, otherwise it will be invalid. Scott Ferguson How to say: "execute the command specified by the string, and then terminate."

#3 building

Although technically it doesn't answer the question directly, it does answer how to do what the original poster wanted to do: composite documents. If so, this is a post to help novices understand what Instance Hunter and Konstantin are talking about.

This is the method I use to combine files (jpg and zip in this case). Note that I created a buffer filled with the contents of the zip file (in small chunks rather than in a large read operation), and then the buffer was written to the back of the JPG file until the end of the zip file was 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);
            }
        }
    }
}

#4 building

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;
Process.Start(proc1);

#5 building

Tried the @ RameshVel solution, but I couldn't pass parameters in my console application. If someone has the same problem, this is a solution:

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.Start();

cmd.StandardInput.WriteLine("echo Oscar");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());

Tags: Windows shell

Posted on Sat, 11 Jan 2020 02:10:39 -0500 by softnmedia