本文主要是介绍WPF 两个程序之间传递参数(shell32.dll),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
当前文章只是笔记,代码并不完善仅作参考。
完整案例:WPF 两个程序之间传递参数(Process)_wpf的exe程序传入参数-CSDN博客
主窗口
[DllImport("shell32.dll")]public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);public void sc(string exepath, string sof_Name){ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(exepath + "\\rest.exe"), new StringBuilder("shortcut " + exepath + " " + sof_Name), new StringBuilder(exepath), 1);//ShortCut.CreateShortCut(exepath, sof_Name);}//例:该方法可以嵌套在按钮的单击事件里面public void StartProgram(){string url = @"F:\MainWindow.exe";//这里是要打开的程序路径,不是当前的程序路径Student stu = new Student();stu.ID = 1;stu.Name = "张三";stu.Age = 18;//把Student对象序列化为Json字符串 //引用:System.Web.Script.Serialization;string paras = new JavaScriptSerializer().Serialize(stu);//获取启动应用程序的可执行文件的路径,包括可执行文件的名称。 就是可以获取到当前程序的.exe文件string exe_path = System.Windows.Forms.Application.ExecutablePath; //System.Diagnostics.Process.GetCurrentProcess//启动MainWindow程序ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(url), new StringBuilder(paras.Replace("\"", "\\\"") + " " + exe_path), new StringBuilder(url.Replace("MainWindow.exe", string.Empty)), 1);//关闭当前程序//this.Close();}class Student{public int ID { get; set; }public string Name { get; set; }public int Age { get; set; }}
要打开的外部窗口(MainWindow)
创建Program.cs类
namespace MainWindow
{class Program{//标注主线程为STA模型[STAThread]static void Main(string[] args){MessageBox.Show("是否可以获取到参数:"+args.Length );bool flag;Application.EnableVisualStyles();//启用可视化样式Application.SetCompatibleTextRenderingDefault(false);//将某些控件上定义的 UseCompatibleTextRendering 属性设置为应用程序范围内的默认值。using (new System.Threading.Mutex(true, Application.ProductName, out flag)){if (flag){if (args.Length > 0){}else{MessageBox.Show("应用程序已经在运行中...");Environment.Exit(1);}}}}}
}
这篇关于WPF 两个程序之间传递参数(shell32.dll)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!