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

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

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


当前回答

还提到了微软。VisualBasic

Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);

其他回答

尝试RameshVel的解决方案,但我无法在我的控制台应用程序中传递参数。如果有人遇到同样的问题,这里有一个解决方案:

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

你可以通过以下方法来实现这一点(在其他答案中提到过):

strCmdText = "'/C some command";
Process.Start("CMD.exe", strCmdText);

当我尝试上面列出的方法时,我发现我的自定义命令不能使用上面一些答案的语法。

我发现更复杂的命令需要封装在引号中才能工作:

string strCmdText;
strCmdText = "'/C cd " + path + " && composer update && composer install -o'";
Process.Start("CMD.exe", strCmdText);
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);

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

这也可以通过P/调用C标准库的系统函数来实现。

using System.Runtime.InteropServices;

[DllImport("msvcrt.dll")]
public static extern int system(string format);

system("copy Test.txt Test2.txt");

输出:

      1 file(s) copied.