我正在處理一個項目,您將一個命令「發送」到cmd.exe並接收輸出。對於這個命令,你需要一個文件路徑-k和一個url。 我有以下代碼(名稱和值變化):cmd進程將不會執行正確的命令
string path = "C:\Users\program.exe"
string pathcustom = "\"" + path + "\""; //the path needs to be in quotation marks
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
string Address = "1.2.3"
string command = pathcustom + " " + "-k" + " " + "https://username:[email protected]" + Address; //Serveradress is the URL
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string ReturnValue = p.StandardOutput.ReadToEnd();
該代碼是工作的罰款就像我希望它是,但我需要另一個梅索德那除了地址如下不同的完全相似。在上面的代碼中,它看起來像1.2.3,但int如下方法Address必須看起來像這樣(包括反斜槓和引號)\「ab:cd:de \」所以我們假設這是
string path = "C:\Users\program.exe"
string pathcustom = "\"" + path + "\"";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
string Address = @"\""ab:cd:de\""";
string command = pathcustom + " " + "-k" + " " + "https://username:[email protected]" + Address;
p.StartInfo.Arguments = "/C " + command;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string ReturnValue = p.StandardOutput.ReadToEnd();
當我重寫代碼以使cmd保持打開狀態時,使用第一種方法獲得我想要的輸出。但是,第二種方法不工作,它將命令發送到cmd並執行它,但是它將「message」寫爲命令被寫入錯誤或無法找到。但是當我使用完全相同的代碼(通過streamwriter將cmd的命令寫入文本文件)並將其複製到cmd中時,它會像執行它一樣執行它。所以基本上,如果我通過c#執行命令,它就不起作用。請幫助
如果你想,只有在不同的Address''價值的另一種方法,請你只讓第一個更普遍的加入'Address'到方法輸入。 –
第二個'command'裏面會有參數'-khttps://用戶名:passwort @serveradress \\\「ab:cd:de \\\」「''是否真的有效的URL? –
我忘了添加一個空格在-k之後,但是我的代碼得到了它。地址在metod輸入中,但是(爲了簡化)我這樣寫了這個問題 – Shmosi