53 lines
2.0 KiB
C#
53 lines
2.0 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// EXEの同期起動(終了待ち)
|
|
/// </summary>
|
|
/// <param name="exePath">実行するEXEパス</param>
|
|
/// <param name="arguments">起動時引数</param>
|
|
/// <param name="retCode">終了コード</param>
|
|
/// <param name="resultMessage">メッセージ</param>
|
|
/// <returns>成否</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|