zoukankan      html  css  js  c++  java
  • 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的方法)。

  • 相关阅读:
    苹果推送通知服务(APNs)编程
    Mac svn命令 linux同样适用
    IOS多线程(NSThread,NSOperation,Grand Central Dispatch)
    iOS7新特性之二维码生成于读取
    Socket即时通讯小实例
    iOS内置加速计(UIAccelerometer/CoreMotion)
    iOS设计模式----委托模式
    NSXMLParser详解
    Core Foundation 框架
    UIView和CALayer的区别
  • 原文地址:https://www.cnblogs.com/runningRain/p/5947610.html
Copyright © 2011-2022 走看看