WPF 进程间传递参数
在软件开发中有时需要在一个软件中启动另一个软件,这时用Process.Start(“软件路径”)可以启动另一个软件。如果在这个过程中还需要传递一些参数给新启动的软件,可以通过WPF中的Application_Startup来完成:
首先,在需要启动的WPF项目中的APP中注册Application_Startup事件:
private void Application_Startup(object sender, StartupEventArgs e) { if (e.Args.Length == 1) { String s = e.Args[0]; } }
这样字符串s就可以得到主程序传过来的参数,可以把这个参数放在Properties中供MainPage调用:


修改App中代码为:
private void Application_Startup(object sender, StartupEventArgs e) { if (e.Args.Length == 1) { this.Properties["Test"] = e.Args[0]; } }
在MainPage中:
txt.Text = Application.Current.Properties["Test"].ToString();
那么主程序如何在启动附程序时传递参数呢?
private void Button_Click_1(object sender, RoutedEventArgs e) { string path = Directory.GetCurrentDirectory() + @"WpfApplication1.exe"; if (string.IsNullOrEmpty(path) || !File.Exists(path)) return; Process.Start(path,"测试字符串"); }