• WPF 程序中启动和关闭外部.exe程序


     当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来:

    C#后台代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication2
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                System.Diagnostics.Process.Start(@"C:Program FilesMicrosoft OfficeOffice14EXCEL.EXE");    //调用该命令,在程序启动时打开Excel程序
            }
        }
    }

    C#后台代码:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace WpfApplication2
    {
        /// <summary>
        /// App.xaml 的交互逻辑
        /// </summary>
        public partial class App : Application
        {
            protected override void OnExit(ExitEventArgs e)     //该重写函数实现在程序退出时关闭某个进程
            {
                Process []myProgress;
                myProgress=Process.GetProcesses();          //获取当前启动的所有进程
                foreach(Process p in myProgress)            //关闭当前启动的Excel进程
                {
                    if (p.ProcessName == "EXCEL")          //通过进程名来寻找
                    {
                        p.Kill();
                        return;
                    }                  
                }
                base.OnExit(e);
            }
        }
    }

    说明:在WPF程序中,Application类的作用有:

        (1)可以跟踪应用程序的生存周期;

        (2)共享应用程序范围的属性和资源;

        (3)管理独立应用程序中的窗口;

    补充:对于Application类,可以有很多属性、事件和方法供调用。当需要实现自定义功能时,可以override方法(标记为Virtual的方法)。

  • 相关阅读:
    PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
    TP5安装workerman版本的坑
    下载git2.2.1并将git添加到环境变量中
    RedHat安装git报错 expected specifier-qualifier-list before ‘z_stream’
    Git出现fatal: Unable to find remote helper for 'https'
    ThinkPHP5实现定时任务
    php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期
    git 查看日志记录
    程序员必读之软件架构 读书笔记
    centos7 安装桌面
  • 原文地址:https://www.cnblogs.com/runningRain/p/5947610.html
Copyright © 2020-2023  润新知