有没有办法在c#应用程序中运行命令提示符命令?如果是这样,我该如何做以下几点:

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

这基本上是在JPG图像中嵌入一个RAR文件。我只是想知道在c#中是否有一种方法可以自动做到这一点。


当前回答

是的,有(参见Matt Hamilton的评论中的链接),但是使用。net的IO类会更简单更好。您可以使用File。ReadAllBytes读取文件,然后是File。写“嵌入式”版本。

其他回答

是的,有(参见Matt Hamilton的评论中的链接),但是使用。net的IO类会更简单更好。您可以使用File。ReadAllBytes读取文件,然后是File。写“嵌入式”版本。

这是所有你要做的从c#运行shell命令

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

编辑:

这是为了隐藏cmd窗口。

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

编辑2:

参数以/C开头是很重要的,否则就行不通。正如@scott-ferguson所说:/C执行字符串指定的命令,然后终止。

你可以在一行中使用CliWrap来做到这一点:

var result = await Cli.Wrap("cmd")
    .WithArguments("copy /b Image1.jpg + Archive.rar Image2.jpg")
    .ExecuteBufferedAsync();

var stdOut = result.StandardOutput;

如果您想在异步模式下运行该命令-并打印结果。你可以学习这门课:

    public static class ExecuteCmd
{
    /// <summary>
    /// Executes a shell command synchronously.
    /// </summary>
    /// <param name="command">string command</param>
    /// <returns>string, as output of the command.</returns>
    public static void ExecuteCommandSync(object command)
    {
        try
        {
            // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
            // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
            // The following commands are needed to redirect the standard output. 
            //This means that it will be redirected to the Process.StandardOutput StreamReader.
            procStartInfo.RedirectStandardOutput =  true;
            procStartInfo.UseShellExecute = false;
            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();

            // Get the output into a string
            string result = proc.StandardOutput.ReadToEnd();

            // Display the command output.
            Console.WriteLine(result);
        }
        catch (Exception objException)
        {
            // Log the exception
            Console.WriteLine("ExecuteCommandSync failed" + objException.Message);
        }
    }

    /// <summary>
    /// Execute the command Asynchronously.
    /// </summary>
    /// <param name="command">string command.</param>
    public static void ExecuteCommandAsync(string command)
    {
        try
        {
            //Asynchronously start the Thread to process the Execute command request.
            Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
            //Make the thread as background thread.
            objThread.IsBackground = true;
            //Set the Priority of the thread.
            objThread.Priority = ThreadPriority.AboveNormal;
            //Start the thread.
            objThread.Start(command);
        }
        catch (ThreadStartException )
        {
            // Log the exception
        }
        catch (ThreadAbortException )
        {
            // Log the exception
        }
        catch (Exception )
        {
            // Log the exception
        }
    }

}

你可以使用RunProcessAsTask包,像这样轻松地异步运行你的进程:

var processResults = await ProcessEx.RunAsync("git.exe", "pull");
//get process result
foreach (var output in processResults.StandardOutput)
{
   Console.WriteLine("Output line: " + output);
}