Run Command Prompt Commands

前端 未结 14 1810
暖寄归人
暖寄归人 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 05:59

    This can also be done by P/Invoking the C standard library's system function.

    using System.Runtime.InteropServices;
    
    [DllImport("msvcrt.dll")]
    public static extern int system(string format);
    
    system("copy Test.txt Test2.txt");
    

    Output:

          1 file(s) copied.
    
    0 讨论(0)
  • 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);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:05

    if you want to keep the cmd window open or want to use it in winform/wpf then use it like this

        string strCmdText;
    //For Testing
        strCmdText= "/K ipconfig";
    
     System.Diagnostics.Process.Start("CMD.exe",strCmdText);
    

    /K

    Will keep the cmd window open

    0 讨论(0)
  • 2020-11-21 06:06

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

    0 讨论(0)
  • 2020-11-21 06:08
    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);
    
    0 讨论(0)
  • 2020-11-21 06:13

    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.

    0 讨论(0)
提交回复
热议问题