有什麼辦法可以在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#
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();
是的,有(請參閱Matt Hamilton的評論中的鏈接),但使用.NET的IO類會更容易和更好。您可以使用File.ReadAllBytes來讀取文件,然後使用File.WriteAllBytes來編寫「嵌入」版本。
將整個文件加載到內存只是爲了追加到另一個不是很有效,特別是如果文件足夠大。 – 2009-09-24 05:34:39
試着看看答案的精神。重點在於.NET具有足夠的IO類和功能來完成此操作,而無需調用OS shell。我提到的特定功能可能不是最好的,但這些只是最簡單的功能。根本沒有任何意義可以呼叫shell來做到這一點。 – 2009-09-25 18:01:52
雖然在技術上這並不直接回答提出的問題,它回答的怎麼辦樓主想做什麼的問題:合併文件。如果有的話,這是一個幫助新手理解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);
}
}
}
}
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);
嘗試@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());
好吧,我沒有機會認爲在我的機器上有一些管理員或反病毒限制,但..上面的代碼工作!感謝Ogglas – 2015-12-30 15:48:55
這一行:cmd.StartInfo.CreateNoWindow = true;拯救了我的一天。 – 2016-02-08 05:23:41
這是我認爲最好的答案 – zezba9000 2016-06-19 19:38:07
這裏是簡單和少代碼版本。它將與參考隱藏控制檯窗口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();
這已經張貼在 – cristi71000 2016-09-05 12:14:55
Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);
上述答案的幫助出於某種原因,它似乎像他們掃到地毯下錯誤,並進行故障排除一個人的命令困難。所以我結束了像這樣下去,也許這將幫助別人:
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();
以上如果我想編譯成一個獨立的控制檯應用程序,還需要添加其他代碼才能使其工作? (我是所有這些編程的東西的小白,只做了一些腳本)。我正在使用csc.exe btw。 – 2017-05-08 04:52:31
@copyitright命名空間和類。如果您只是創建一個新項目,它們將爲您生成。 – coinbird 2017-08-28 16:01:03
您可以在同一行做到這一點使用CliWrap:
var stdout = new Cli("cmd")
.Execute("copy /b Image1.jpg + Archive.rar Image2.jpg")
.StandardOutput;
你可以做到這一點通過以下方法(在其他的答案提到):
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);
重複http://stackoverflow.com/questions/181719/how-to-start-a-process-from的-c-winforms(有一個答案就是你想要的)。 – 2009-09-24 04:27:21
http://stackoverflow.com/a/5367686/492有更好的答案 – 2015-08-17 00:22:12