C# 使用 Process和ProcessStartInfo達到A程式呼叫B程式[CS_Process_ProcessStartInfo]

C# 使用 Process和ProcessStartInfo達到A程式呼叫B程式[CS_Process_ProcessStartInfo]

C# 使用 Process和ProcessStartInfo達到A程式呼叫B程式[CS_Process_ProcessStartInfo]


GITHUB: https://github.com/jash-git/CS_Process_ProcessStartInfo


目的:

    A程式呼叫B程式時傳送參數,並等待B程式執行結束
    B程式執行結束後,A程式讀取執行結果


CS_Process01

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_Process01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = "CS_Process02";  // Specify exe name.
            start.Arguments = "F00000000000000E";
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;

            Process p = Process.Start(start);
            if (p != null)
            {
                p.WaitForExit();
                StreamReader reader = p.StandardOutput;
                string result=p.StandardOutput.ReadToEnd();
                MessageBox.Show(String.Format("外部程式 {0} 已經退出!\n 回傳值={1}", start.FileName, result), this.Text,
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}


CS_Process02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_Process02
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            String StrArgs = "";
            for (int i = 0; i < args.Length; i++)
            {
                if (i == 0)
                {
                    StrArgs = args[i];
                }
                else
                {
                    StrArgs += "-" + args[i];
                }

            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(StrArgs));
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_Process02
{
    public partial class Form1 : Form
    {
        public String m_StrCmdInput = "";
        public Form1(String StrCmdInput = "")
        {
            InitializeComponent();
            m_StrCmdInput = StrCmdInput;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(m_StrCmdInput.Length>0)
            {
                this.Text += " ~" + m_StrCmdInput;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Console.Write(m_StrCmdInput+","+m_StrCmdInput);
        }
    }
}

One thought on “C# 使用 Process和ProcessStartInfo達到A程式呼叫B程式[CS_Process_ProcessStartInfo]

  1. C# Process ProcessStartInfo 呼叫程式 EXE 指定 工作目錄

    String StrWorkingDirectory = FileLib.path + "\\SYRIS CPU Card Tool\\";
    String StrExe = StrWorkingDirectory+"SYRIS CPU Card Tool.exe";

    ProcessStartInfo start = new ProcessStartInfo();
    start.WorkingDirectory = StrWorkingDirectory;
    start.FileName = StrExe; // Specify exe name.
    start.Arguments = labSub010200_08.Text + ";" + cmbSub010200_01.SelectedIndex;//參數
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;//接收執行結果

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *