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);
}
}
}
6 thoughts on “C# 使用 Process和ProcessStartInfo達到A程式呼叫B程式[CS_Process_ProcessStartInfo]”
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;//接收執行結果
C# Process ProcessStartInfo 呼叫程式 EXE 指定 工作目錄 & 不要顯示UI/不要顯示畫面
private static bool CallNCCC()//呼叫NCCC.EXE
{
bool blnResult = false;
try
{
//---
//呼叫NCCC.exe
String StrFilePath = m_StrSysPath + "NCCC.exe";
ProcessStartInfo start = new ProcessStartInfo();
start.WorkingDirectory = m_StrSysPath;
start.FileName = StrFilePath;
start.CreateNoWindow = true;//不要有UI閃爍[CMD不要建立UI]
start.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = Process.Start(start);
if (proc != null)
{
proc.WaitForExit();
blnResult = true;
}
//---呼叫NCCC.exe
}
catch (Exception e)
{
LogFile.Write("NCCCAPI.CallNCCC[C# ERROR]" + e.ToString());
}
return blnResult;
}
c# processstartinfo 指定工作目錄 對應 delphi 語法 [Gemini]
var
StartInfo: TProcessStartInfo;
begin
StartInfo := TProcessStartInfo.Create;
try
StartInfo.FileName := 'myprogram.exe';
StartInfo.WorkingDirectory := 'C:\MyWorkingDirectory';
StartInfo.Create(StartInfo);
finally
StartInfo.Free;
end;
end;
ar
StartInfo: TProcessStartInfo;
begin
StartInfo := TProcessStartInfo.Create;
try
StartInfo.FileName := 'myprogram.exe';
StartInfo.WorkingDirectory := 'C:\MyWorkingDirectory';
StartInfo.Create(StartInfo);
finally
StartInfo.Free;
end;
end;
delphi TProcess Work Directory [Gemini]
var
StartInfo: TProcessStartInfo;
begin
StartInfo := TProcessStartInfo.Create;
try
StartInfo.FileName := 'myprogram.exe'; // Replace with your program name
StartInfo.WorkingDirectory := 'C:\MyWorkingDirectory'; // Set your desired directory
TProcess.Create(StartInfo); // Start the process
finally
StartInfo.Free;
end;
end;
C++ shellexecute api Set work directory [Gemini]
#include
int main() {
// Replace with your program name and desired working directory
const char* program = "myprogram.exe";
const char* workingDir = "C:\\MyWorkingDirectory";
// Launch the program with the specified working directory
ShellExecute(NULL, NULL, program, NULL, workingDir, SW_SHOWDEFAULT);
return 0;
}