2009-09-24 191 views
432

有什么办法可以在C#应用程序中运行命令提示符命令吗?如果是这样,我将如何做到以下几点:运行命令提示符命令

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

这基本上嵌入一个RAR文件在JPG图像。我只是想知道是否有一种方法可以在C#中自动执行此操作。谢谢。

+6

重复http://stackoverflow.com/questions/181719/how-to-start-a-process-from的-c-winforms(有一个答案就是你想要的)。 – 2009-09-24 04:27:21

+0

http://stackoverflow.com/a/5367686/492有更好的答案 – 2015-08-17 00:22:12

回答

679

这是所有你必须从C#

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

做运行shell命令编辑:

这是隐藏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(); 
+11

这很好!我可以问一下“/ C”的用途吗? – user 2009-09-24 04:42:22

+124

/C执行字符串指定的命令,然后终止 – 2009-09-24 04:48:18

+11

它只是为了告诉cmd运行和终止(不要等待任何用户输入来关闭窗口) – RameshVel 2009-09-24 04:49:08

8

是的,有(请参阅Matt Hamilton的评论中的链接),但使用.NET的IO类会更容易和更好。您可以使用File.ReadAllBytes来读取文件,然后使用File.WriteAllBytes来编写“嵌入”版本。

+10

将整个文件加载到内存只是为了追加到另一个不是很有效,特别是如果文件足够大。 – 2009-09-24 05:34:39

+5

试着看看答案的精神。重点在于.NET具有足够的IO类和功能来完成此操作,而无需调用OS shell。我提到的特定功能可能不是最好的,但这些只是最简单的功能。根本没有任何意义可以呼叫shell来做到这一点。 – 2009-09-25 18:01:52

10

虽然在技术上这并不直接回答提出的问题,它回答的怎么办楼主想做什么的问题:合并文件。如果有的话,这是一个帮助新手理解Instance Hunter和Konstantin正在谈论的帖子。

这是我用来组合文件的方法(在本例中为jpg和zip)。请注意,我创建了一个缓冲区,该缓冲区充满了zip文件的内容(以小块而不是一次大的读取操作),然后将缓冲区写入jpg文件的后面,直到zip文件的结尾为止达:

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); 
      } 
     } 
    } 
} 
21
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

C#中的@符号是什么? – Pacerier 2015-04-02 10:30:36

+5

@Pacerier它告诉编译器转义所有通常必须在字符串中转义的字符,在本例中为\。因此,没有\,你的代码看起来像'proc1.FileName =“C:\\ Windows \\ System32 \\ cmd.exe”;' – 2015-04-03 01:42:59

+0

@JamesKo,啊发现它:http://stackoverflow.com/q/556133/632951。 Google在符号上仍然很糟糕。 – Pacerier 2015-04-06 13:54:30

68

尝试@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()); 
+2

好吧,我没有机会认为在我的机器上有一些管理员或反病毒限制,但..上面的代码工作!感谢Ogglas – 2015-12-30 15:48:55

+4

这一行:cmd.StartInfo.CreateNoWindow = true;拯救了我的一天。 – 2016-02-08 05:23:41

+3

这是我认为最好的答案 – zezba9000 2016-06-19 19:38:07

5

这里是简单和少代码版本。它将与参考隐藏控制台窗口too-

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
process.StartInfo.FileName = "cmd.exe"; 
process.StartInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; 
process.Start(); 
+0

这已经张贴在 – cristi71000 2016-09-05 12:14:55

5

上述答案的帮助出于某种原因,它似乎像他们扫到地毯下错误,并进行故障排除一个人的命令困难。所以我结束了像这样下去,也许这将帮助别人:

var proc = new Process 
{ 
    StartInfo = new ProcessStartInfo 
    { 
     FileName = @"C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE\tf.exe", 
     Arguments = "checkout AndroidManifest.xml", 
     UseShellExecute = false, 
     RedirectStandardOutput = true, 
     CreateNoWindow = true, 
     WorkingDirectory = @"C:\MyAndroidApp\" 
    } 
}; 

proc.Start(); 
+0

以上如果我想编译成一个独立的控制台应用程序,还需要添加其他代码才能使其工作? (我是所有这些编程的东西的小白,只做了一些脚本)。我正在使用csc.exe btw。 – 2017-05-08 04:52:31

+0

@copyitright命名空间和类。如果您只是创建一个新项目,它们将为您生成。 – coinbird 2017-08-28 16:01:03

2

您可以在同一行做到这一点使用CliWrap

var stdout = new Cli("cmd") 
     .Execute("copy /b Image1.jpg + Archive.rar Image2.jpg") 
     .StandardOutput; 
0

你可以做到这一点通过以下方法(在其他的答案提到):

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