Run Command Prompt Commands

前端 未结 14 1960
暖寄归人
暖寄归人 2020-11-21 05:32

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.jp         


        
14条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 06:03

    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. If anything, this is a post to help newbies understand what Instance Hunter and Konstantin are talking about.

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

提交回复
热议问题