2010-06-15 136 views
2

我需要從My C#應用程序編譯C++程序。在我按下按鈕時,我需要打開cl.exe。任何人都可以給我一些示例代碼。從C#應用程序打開cl.exe

+1

?執行cl.exe與執行任何其他進程沒有區別... – 2010-06-15 04:48:31

+0

對不起,我無法得到它。 – Kasun 2010-06-15 04:51:00

+0

http://www.csharp-station.com/howto/processstart.aspx – 2010-06-15 04:57:43

回答

3
string filePath = "c:\program files\xyz\cl.exe"; 
System.Diagnostics.Process.Start(filePath); 
+0

嗨,當我使用這種方法時,它將顯示以下錯誤。如何傳遞樁名CL.EXE System.ComponentModel.Win32Exception了未處理 消息=「系統找不到指定文件」 源=「系統」 錯誤碼= -2147467259 NativeErrorCode = 2 堆棧跟蹤: – Kasun 2010-06-15 05:23:39

+0

放入CL.exe的完整路徑,例如「c:\ program files \ xyz \ cl.exe」 – 2010-06-15 07:24:37

+0

這不是正確的答案,cl.exe需要將mspdb100.dll(和其他一些項目)放在%PATH%中。我在下面概述了真正的解決方案。 – 2013-08-29 04:18:28

1

如果你想打開一些EXE可以使用:Process.Start("cl.exe");

+0

嗨,當我使用這種方法時,它會顯示以下錯誤。我怎樣才能通過堆名稱爲CL.​​exe System.ComponentModel.Win32Exception是未處理消息=「系統找不到指定的文件」Source =「System」ErrorCode = -2147467259 NativeErrorCode = 2 StackTrace – Kasun 2010-06-15 05:23:59

+1

您需要提及正確的路徑的exe。 – anishMarokey 2010-06-15 06:31:44

0

cl.exe時需要DLL的不在%PATH%。您需要爲架構執行正確的批處理(x86與64位)。這將更新您的環境,以便您可以正確啓動cl.exe。

86就設在這裏(VS2010):

  • C:\ Program Files文件(x86)的\微軟的Visual Studio 10.0 \ VC \ BIN \ VCVARS32.BAT

我目前啓動命令控制檯,執行批處理,然後在我的臨時文件上啓動編譯。

string tempPath = Path.GetTempPath(); 
string tempName = "scrap_" + random.Next(); 

Process compiler = new Process(); 

compiler.StartInfo.FileName = "cmd.exe"; 
compiler.StartInfo.WorkingDirectory = tempPath; 
compiler.StartInfo.RedirectStandardInput = true; 
compiler.StartInfo.RedirectStandardOutput = true; 
compiler.StartInfo.UseShellExecute = false; 

compiler.Start(); 
compiler.StandardInput.WriteLine("\"" + @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat" + "\""); 
compiler.StandardInput.WriteLine(@"cl.exe /nologo /EHsc " + tempName + ".cpp"); 
compiler.StandardInput.WriteLine(@"exit"); 
string output = compiler.StandardOutput.ReadToEnd(); 
compiler.WaitForExit(); 
Debug.Write(output); 
compiler.Close(); 

我目前正在研究直接啓動VCVARS32.BAT,但我不知道這是否正確更新環境對我的C#應用​​程序。我會更新你,如果我得到它的工作。

此外,請確保您已閱讀並理解這一主題接受的答案:你嘗試過什麼ProcessStartInfo hanging on "WaitForExit"? Why?