有没有办法在c#应用程序中运行命令提示符命令?如果是这样,我该如何做以下几点:
copy /b Image1.jpg + Archive.rar Image2.jpg
这基本上是在JPG图像中嵌入一个RAR文件。我只是想知道在c#中是否有一种方法可以自动做到这一点。
有没有办法在c#应用程序中运行命令提示符命令?如果是这样,我该如何做以下几点:
copy /b Image1.jpg + Archive.rar Image2.jpg
这基本上是在JPG图像中嵌入一个RAR文件。我只是想知道在c#中是否有一种方法可以自动做到这一点。
当前回答
虽然从技术上讲,这并没有直接回答提出的问题,但它确实回答了如何做原始海报想做的事情:合并文件。如果有的话,这篇文章是为了帮助新手理解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);
}
}
}
}
其他回答
是的,有(参见Matt Hamilton的评论中的链接),但是使用。net的IO类会更简单更好。您可以使用File。ReadAllBytes读取文件,然后是File。写“嵌入式”版本。
如果你想保持CMD窗口打开,或者想在winform/wpf中使用它,那么就像这样使用它
string strCmdText;
//For Testing
strCmdText= "/K ipconfig";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
/K
将保持cmd窗口打开吗
你可以使用RunProcessAsTask包,像这样轻松地异步运行你的进程:
var processResults = await ProcessEx.RunAsync("git.exe", "pull");
//get process result
foreach (var output in processResults.StandardOutput)
{
Console.WriteLine("Output line: " + output);
}
如果您想在异步模式下运行该命令-并打印结果。你可以学习这门课:
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
}
}
}
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);