C# 运行命令提示符命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1469764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Command Prompt Commands
提问by user
Is there any way to run command prompt commands from within a C# application? If so how would I do the following:
有没有办法从 C# 应用程序中运行命令提示符命令?如果是这样,我将如何执行以下操作:
copy /b Image1.jpg + Archive.rar Image2.jpg
This basically embeds an RAR file within JPG image. I was just wondering if there was a way to do this automatically in C#.
这基本上是在 JPG 图像中嵌入了一个 RAR 文件。我只是想知道是否有办法在 C# 中自动执行此操作。
采纳答案by RameshVel
this is all you have to do run shell commands from C#
这就是你从 C# 运行 shell 命令所要做的一切
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.
这是为了隐藏 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();
EDIT: 2
编辑:2
Important is that the argument begins with /C
otherwise it won't work. How Scott Fergusonsaid: it "Carries out the command specified by the string and then terminates."
重要的是,论点以 开头,/C
否则它将不起作用。Scott Ferguson怎么说:它“执行字符串指定的命令,然后终止。”
回答by Instance Hunter
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.
是的,有(参见 Matt Hamilton 评论中的链接),但使用 .NET 的 IO 类会更容易也更好。您可以使用 File.ReadAllBytes 读取文件,然后使用 File.WriteAllBytes 编写“嵌入式”版本。
回答by CarllDev
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.
尽管从技术上讲这并不能直接回答提出的问题,但它确实回答了如何做原始海报想要做的事情的问题:合并文件。如果有的话,这篇文章可以帮助新手了解 Instance Hunter 和 Konstantin 在谈论什么。
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:
这是我用来组合文件的方法(在这种情况下是 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);
}
}
}
}
回答by HackerMan
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);
回答by Ogglas
Tried @RameshVel solution but I could not pass arguments in my console application. If anyone experiences the same problem here is a solution:
尝试了@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());
回答by kamalpreet
Here is little simple and less code version. It will hide the console window 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();
回答by Slai
with a reference to Microsoft.VisualBasic
Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);
回答by Matt Bonness
None of the above answers helped for some reason, it seems like they sweep errors under the rug and make troubleshooting one's command difficult. So I ended up going with something like this, maybe it will help someone else:
出于某种原因,上述答案都没有帮助,似乎它们将错误扫到了地毯下,并使对命令进行故障排除变得困难。所以我最终做了这样的事情,也许它会帮助别人:
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();
回答by Tyrrrz
回答by Vladimir verleg
You can achieve this by using the following method (as mentioned in other answers):
您可以使用以下方法(如其他答案中所述)来实现此目的:
strCmdText = "'/C some command";
Process.Start("CMD.exe", strCmdText);
When I tried the methods listed above I found that my custom command did not work using the syntax of some of the answers above.
当我尝试上面列出的方法时,我发现我的自定义命令无法使用上面一些答案的语法。
I found out more complex commands need to be encapsulated in quotes to work:
我发现需要将更复杂的命令封装在引号中才能工作:
string strCmdText;
strCmdText = "'/C cd " + path + " && composer update && composer install -o'";
Process.Start("CMD.exe", strCmdText);