簡體   English   中英

從Visual Studio打開的CMD提示符不會在我的計算機上啟動exe。 為什么不?

[英]CMD prompt opened from Visual Studio won't launch a exe on my computer. Why not?

我有以下代碼來啟動cmd提示符,並從該cmd提示符啟動putty.exe和最后的IP地址。

private void btnRouter_Click(Object sender, EventArgs e)
{
    MessageBox.Show((string)((Button)sender).Tag);
    System.Diagnostics.Process cmd = new System.Diagnostics.Process();
    cmd.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
    cmd.StartInfo.UseShellExecute = true;
    cmd.Start();
    cmd.StandardInput.WriteLine("Putty.exe " + ((string)((Button)sender).Tag));
    cmd.WaitForExit();
}

問題是我一直收到錯誤“StandardIn沒有被重定向。” 從Visual Studio中,當我嘗試在命令窗口中輸入putty.exe時,我得到“'putty.exe'不被識別為內部或外部命令,可操作程序或批處理文件。” 這真的很奇怪,因為如果我去運行行,輸入cmd然后putty.exe它立即打開,因為我將putty應用程序的絕對文件夾路徑添加到我的系統環境路徑。

有沒有理由從Visual Studio打開CMD沒有使用我的環境路徑?

仍然不知道為什么會發生這種情況,但是我回到了之前的代碼並將putty.exe的副本放在我的調試文件夾中,這次它成功啟動了。

private void btnRouter_Click(Object sender, EventArgs e)
{
    MessageBox.Show((string)((Button)sender).Tag);
    System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.FileName = "Putty.exe ";
    startInfo.Arguments = ((string)((Button)sender).Tag);
    myProcess.StartInfo = startInfo;
    myProcess.Start();
}

此行將導致“StandardIn尚未重定向”。 因為您嘗試寫入stdin並且該句柄未正確設置輸入錯誤:

cmd.StandardInput.WriteLine("Putty.exe " + ((string)((Button)sender).Tag));

至於這個問題:

有沒有理由從Visual Studio打開CMD沒有使用我的環境路徑?

當父進程啟動子進程時,子進程將繼承其父進程的環境。

換句話說,您啟動的新cmd窗口將繼承 Visual Studio環境,但這並不意味着Visual Studio環境與命令提示符的環境相同。

您可以通過啟動命令行提示符,從該命令行提示符運行Visual Studio,然后創建子cmd進程來對此進行測試。

現在,您的cmd進程應該具有與原始命令行匹配的環境以及Visual Studio添加到其環境副本的任何更改。

使用runas命令。

http://technet.microsoft.com/en-us/library/cc771525.aspx

你能跑dir嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM