c#只启动一个软件并将其置于最前

来源:互联网 发布:如何安装越狱软件 编辑:程序博客网 时间:2024/05/19 18:39
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Diagnostics;using System.Runtime.InteropServices;namespace Test{    public partial class FrontForm : Form    {        [DllImport("user32.dll")]        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);        string processName = "";        private void button1_Click(object sender, EventArgs e)        {            if (processName.Length <= 0)            {                processName = Process.Start("hash.exe").ProcessName;//启动软件时获取进程名称              }            else            {                Process[] temp = Process.GetProcessesByName(processName);//在所有已启动的进程中查找需要的进程;                  if (temp.Length > 0)//如果查找到                  {                    IntPtr handle = temp[0].MainWindowHandle;                    SwitchToThisWindow(handle, true);    // 激活,显示在最前                  }                            }        }        public FrontForm()        {            InitializeComponent();        }    }}

0 0
原创粉丝点击