[英]Unable to execute cmd commands from C#
我想從Visual Studio(使用C#)執行cmd命令。 我要運行兩個命令。
我提到了這篇文章,但不適用於我的情況。
我的代碼是:
private void ExecuteCmdCommands()
{
string strCmdText;
strCmdText = @"cd C:\Test + makecab /f wsp.ddf";
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = strCmdText;
process.StartInfo = startInfo;
process.Start();
}
當我運行此代碼時,僅打開命令提示符,不執行任何命令。 我想念什么?
不要更改目錄,只需shell打開文件:
strCmdText = @"C:\Test + makecab /f wsp.ddf";
編輯:設置工作目錄:
startInfo.WorkingDirectory = @"C:\Test";
為了從一行調用2命令,您需要使用&
符號。
在您的情況下: @"/c cd C:\\Test & makecab /f wsp.ddf";
也不要忘記/c
標志,告訴cmd執行命令。
嘗試更改為strCmdText = @“ C:\\ Test + makecab / f wsp.ddf”;
只是一個猜測,但看起來您正在嘗試在同一行上執行2個不同的命令?
首先無需更改目錄,並且不需要執行cmd.exe。 只需直接為makecab程序創建一個進程即可。
startInfo.Filename = @"makecab.exe";
startInfo.Argumanets = @"/f c:\test\wsp.ddf";
這里的問題是您要傳遞的命令是參數而不是命令(必須通過StandardInput管道進行傳遞)。 幸運的是,您可以使用此處提到的“ / c”參數。 我不確定它是否可以與“ +”運算符一起使用。
請注意,正如其他人所述,您還應該使用available屬性設置工作目錄,否則,如果不使用“ C:”工作目錄運行程序,則程序將失敗。
you have to do run shell commands from C#
string strCmdText;
strCmdText= "/C copy /b Image1.jpg + xyz.rar Image2.jpg"; System.Diagnostics.Process.Start("CMD.exe",strCmdText);
**EDIT:**
This is to hide the cmd window.
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 + xyz.rar Image2.jpg";
process.StartInfo = startInfo;
process.Start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.