search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C# 使用 Process和ProcessStartInfo達到A程式呼叫B程式[CS_Process_ProcessStartInfo] – jashliao部落格

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
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [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);
        }
    }
}



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦