using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace KssSmaPlaLib.Inbox
{
public static class ProcessUxer
{
///
/// EXEの同期起動(終了待ち)
///
/// 実行するEXEパス
/// 起動時引数
/// 終了コード
/// メッセージ
/// 成否
public static bool TryRunExeSync(string exePath, string arguments, out int retCode, out string resultMessage)
{
resultMessage = string.Empty;
retCode = -1;
// StartInfoで起動オプションを細かく制御
var startInfo = new ProcessStartInfo
{
FileName = exePath,
Arguments = arguments,
CreateNoWindow = true, // 背後でこっそり動かす(UXの邪魔をしない)
UseShellExecute = false, // Win32 API直叩きに近い挙動(INI派ならこっち!)
RedirectStandardError = true, // エラー出力を捕まえる準備
RedirectStandardOutput = true, // 標準出力を捕まえる準備
WorkingDirectory = Path.GetDirectoryName(exePath), // 作業ディレクトリを切り替える(EXE格納フォルダ)
};
using var process = Process.Start(startInfo);
if (process == null)
{
resultMessage = "起動(Process.Start())に失敗しました。";
return false;
}
// 【ここが心臓部】完全終了を待つ
process.WaitForExit();
// 終了コードを返して、呼び出し側で「驚きのリカバリ」をさせる
retCode = process.ExitCode;
return true;
}
}
}