tags:

views:

1749

answers:

2

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#. Thank you.

+1  A: 

Yes, there is (see link in Matt Hamilton's comment), but it would be easier and better to use .NET's IO classes. You can use File.ReadAllBytes to read the files and then File.WriteAllBytes to write the "embedded" version.

Daniel Straight
Loading whole files into memory just to append one to another is not very efficient, especially if files are big enough.
Konstantin Spirin
Try to look at the spirit of the answer. The point is that .NET has more than enough IO classes and functions to do this without having to call out to the OS shell. The particular functions I mentioned may not be the best, but those were just the simplest. It doesn't make any sense at all to call out to the shell to do this.
Daniel Straight
+4  A: 

Hi,

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 stratInfo = new System.Diagnostics.ProcessStartInfo();
        stratInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        stratInfo.FileName = "cmd.exe";
        stratInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
        process.StartInfo = stratInfo;
        process.Start();

Cheers

Ramesh Vel

Ramesh Vel
That works great! Can I just ask what the "/C" is for?
Nate Shoffner
/C Carries out the command specified by string and then terminates
Scott Ferguson
its just to tell the cmd to run and terminate (dont wait for any user input to close the window)
Ramesh Vel
Thank you, one more question. Is there a way to hide the the command prompt during this?
Nate Shoffner
@Nate, i have updated my answer for ur comments. pls check out
Ramesh Vel
Thank you so very much. One more question lol, I'm sorry. Currently, the program has to be ran in the same directory that the files are. Is there a way to make it so the files can be in different directories?Examples:C:\Image1.jpg + C:\Directory1\Archive.rar C:\Directory2\Image2.jpg
Nate Shoffner
CMD accpets the complete path... try to execute this in command line iteslf and check.. because normal copy operation like this works "copy d:\web.config d:\w5.config"; perfectly..
Ramesh Vel
Don't I have to change the "C:\" to "C:\\"? I had to do that to get it to run. It still doesn't want to work though...
Nate Shoffner
i just tested this.. "copy /b c:\Image1.jpg + d:\archive.rar d:\image2.jpg".. it works finr
Ramesh Vel
Ok, I found out what the problem is. I folders that the files were in were under the C:\Documents and Settings\ in a subdirectory. I guess it is because of the spaces in the directory name?
Nate Shoffner
I don't see how I'm the only one that thinks this is a horrible idea. Yes, this will work, but it's completely and totally wrong. Spawning CMD processes to do simple IO operations is wrong, even if it works. Read through the documentation on the System.IO namespace. There is more than enough functionality in there to do what you need to do without spawning unneeded processes.
Daniel Straight