C#查找运行中的应用程序并将其显示到前端

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        private const int SW_HIDE = 0;
        private const int SW_NORMAL = 1;
        private const int SW_MAXIMIZE = 3;
        private const int SW_SHOWNOACTIVATE = 4;
        private const int SW_SHOW = 5;
        private const int SW_MINIMIZE = 6;
        private const int SW_RESTORE = 9;
        private const int SW_SHOWDEFAULT = 10;

        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);        

        static void Main(string[] args)
        {
            string processName = "WindowsFormsApp1"; //要查找的进程名称
            Process[] processes = Process.GetProcessesByName(processName);
            if(processes.Length>0)
            {
                ShowWindowAsync(processes[0].MainWindowHandle, SW_SHOW);
                SetForegroundWindow(processes[0].MainWindowHandle);
            }
            else
            {
                Console.WriteLine("程序没有运行!");
                Console.ReadKey();
                //Process.Start("D:\WindowsFormsApp1.exe");
            }
        }
    }
}


评论: 0 | 引用: 0 | 查看次数: 1832
发表评论
登录后再发表评论!