C#/WPF 應用程式A 呼叫WPF編寫的應用程式B的exe時,引數傳遞問題
阿新 • • 發佈:2018-12-17
經過驗證該方法可行,注意:引數可以多個傳入,以空格分隔開的;
WPF中, 應用程式A 呼叫WPF編寫的應用程式B的exe,引數傳遞給B的 主要方法:
1、在App.xaml.cs中的App類中,過載“OnStartup”函式
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { if (e.Args != null && e.Args.Count() > 0) { this.Properties["UserIndex"] = e.Args[0]; MessageBox.Show("外部引數進入app Application 資料:" + e.Args[0].ToString()); } base.OnStartup(e); } }
2、在需要接收的窗體的loaded事件中或者建構函式中接收值
public RealStomachMainViewModel() { if (Application.Current.Properties["UserIndex"] != null) { string temp = Application.Current.Properties["UserIndex"].ToString(); MessageBox.Show("非常完美!!!進入主程 RealStomachMainViewModel 外部引數傳入:" + temp); } }
3、測試程式碼:
private void Button_Click(object sender, RoutedEventArgs e)
{
string path = @"D:\Working\Debug\RealStomach_WPF.exe";
if (File.Exists(path))
{
Process.Start(path, "122");
}
}
4、如果是等待這個EXE程式執行完畢再執行下面的程式碼其實也是很簡單的,只需要在後面使用WaitForExit()方法即可。程式碼如下
System.Diagnostics.Process.Start(Application.StartupPath + “你的應用程式.exe”).WaitForExit();
等待B程式關閉之後,才執行,這句後面的程式碼;