c# Winform 調用可執行 exe 文件


c#是一個寫windows桌面小工具的好東西,但有個時候,我們需要在 winform 程序中調用其他的 exe 文件,那么該如何實現呢?

如果只是拉起一個 exe 文件,可以參考如下方法實現:

string exefile = "xxx.exe";
if (File.Exists(exefile)) {
    Process process = new Process();
   // params 為 string 類型的參數,多個參數以空格分隔,如果某個參數為空,可以傳入”” ProcessStartInfo startInfo
= new ProcessStartInfo(exefile, params); process.StartInfo = startInfo; process.Start(); }

如果不想彈出系統的dos界面,可以參考如下方式實現:

string exefile = "xxx.exe";
if (File.Exists(exefile)) {
    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo(exefile, path);
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true; process.StartInfo
= startInfo; process.Start(); process.WaitForExit(2000); string output = process.StandardOutput.ReadToEnd(); rtb_analyze.Text = output; process.Close(); }

當然還有異步方式:

public void exec(string exePath, string parameters)
{
    Process process = new System.Diagnostics.Process();
    process.StartInfo.FileName = exePath;
    process.StartInfo.Arguments = parameters;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    process.BeginOutputReadLine();
    process.OutputDataReceived += new  DataReceivedEventHandler(processOutputDataReceived);
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM