Zi 字媒體
2017-07-25T20:27:27+00:00
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);
}
}
}
寫了
5860316篇文章,獲得
23313次喜歡