有没有办法从C#应用程序内部运行命令提示符命令?如果可以,我该如何执行以下操作:
copy /b Image1.jpg + Archive.rar Image2.jpg
这基本上是将RAR文件嵌入到JPG图像中。我只是想知道是否有一种在C#中自动完成此操作的方式。
有没有办法从C#应用程序内部运行命令提示符命令?如果可以,我该如何执行以下操作:
copy /b Image1.jpg + Archive.rar Image2.jpg
这基本上是将RAR文件嵌入到JPG图像中。我只是想知道是否有一种在C#中自动完成此操作的方式。
这就是你需要做的,从C#中运行shell命令。
string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
编辑:
这是为了隐藏命令行窗口。
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 执行指定的命令字符串,然后终止。
尝试了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());
cmd.StandardInput.WriteLine(@"cd C:\Test; pwd")
中执行多个命令? - Zach Smithvar 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);
@
符号是一个特殊字符,用于指示后面的字符串应该被处理为字面量,而不需要转义其中包含的特殊字符。 - Pacerierproc1.FileName = "C:\\Windows\\System32\\cmd.exe";
。 - James Koproc1.Verb = "runas";
会使该进程以提升的权限运行... 这并不总是有意为之。 - Dinei由于某些原因,上面的答案都没有帮助,似乎他们会掩盖错误并使命令的故障排除变得困难。所以我最终采用了这样的方法,也许可以帮助其他人:
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();
cmd.exe
应用程序,你可以将命令作为参数传递。 - Zach Smithecho Hello World!
并在弹出的命令窗口中显示命令输出。所以我尝试了以下代码:Filename = @"echo"
,Arguments = "Hello World!"
,UseShellExecute = false
,RedirectStandardOuput = false
,CreateNoWindow = false
。这允许父应用程序的命令窗口显示 "Hello World!"(这是有意义的,因为标准输出未重定向到子进程)。 - Minh Tran虽然严格意义上这并没有直接回答问题,但它确实回答了原帖想要做的事情:合并文件。如果说有什么作用的话,那就是为新手解释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);
}
}
}
}
如果您希望保持cmd窗口打开或者想要在WinForm/WPF中使用它,那么请按如下方式使用。
string strCmdText;
//For Testing
strCmdText= "/K ipconfig";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
/K
将保持命令窗口打开。
如果你想以异步模式运行命令并打印结果,你可以使用这个类:
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
}
}
}
system
函数来实现。using System.Runtime.InteropServices;
[DllImport("msvcrt.dll")]
public static extern int system(string format);
system("copy Test.txt Test2.txt");
输出:
1 file(s) copied.
引用了 Microsoft.VisualBasic
库。
Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);