C# 检查进程是否正在运行。如果有,打开它

C# 检查进程是否正在运行。如果有,打开它,c#,windows,winforms,C#,Windows,Winforms,好吧,假设我运行了一个进程 让我们说,“Skype”。我想点击C#Windows窗体应用程序中的按钮 当我点击按钮(让我们调用按钮“Skype运行”) 所以,当我点击“Skype跑步”时。它将检查进程当前是否正在运行 如果进程未运行,则假定它将执行以下操作: MessageBox.Show("Sorry! Skype is not running.") 但是。如果它正在运行,它将打开它,成为您当前正在查看的窗口 因此,如果您在Word中,并单击“Skype跑步”。Skype正在运行,它打开了窗

好吧,假设我运行了一个
进程

让我们说,“Skype”。我想点击C#Windows窗体应用程序中的
按钮

当我点击
按钮
(让我们调用
按钮
“Skype运行”)

所以,当我点击“Skype跑步”时。它将检查进程当前是否正在运行

如果进程未运行,则假定它将执行以下操作:

MessageBox.Show("Sorry! Skype is not running.")
但是。如果它正在运行,它将打开它,成为您当前正在查看的窗口

因此,如果您在Word中,并单击“Skype跑步”。Skype正在运行,它打开了窗口。(就像在windows的任务管理器中执行“切换到”。)


如果我没有很好地解释我的问题,请告诉我。我真的需要答案:)

您可以使用
process.getProcessByName
静态方法检查进程是否已经在运行


使用Win API可以将特定窗口置于最前面,您可以使用
进程.getProcessByName
静态方法检查进程是否已在运行


使用Win API可以实现将特定窗口置于前端。这里有一个代码示例,演示了如何执行此操作:

下面文章中的相关代码示例。这显示了如何在button1_Click事件处理程序中查找进程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //required for APIs
namespace Find

{

public partial class Form1 : Form
{
    //Import the FindWindow API to find our window
    [DllImportAttribute("User32.dll")]
    private static extern int FindWindow(String ClassName, String WindowName);

    //Import the SetForeground API to activate it
    [DllImportAttribute("User32.dll")]
    private static extern IntPtr SetForegroundWindow(int hWnd);


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Find the window, using the CORRECT Window Title, for example, Notepad
        int hWnd = FindWindow(null, "Untitled - Notepad");
        if (hWnd > 0) //If found
        {
            SetForegroundWindow(hWnd); //Activate it
        }
        else
        {
            MessageBox.Show("Window Not Found!");
        }


    }
}
}

这里有一个代码示例,演示了如何执行此操作:

下面文章中的相关代码示例。这显示了如何在button1_Click事件处理程序中查找进程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //required for APIs
namespace Find

{

public partial class Form1 : Form
{
    //Import the FindWindow API to find our window
    [DllImportAttribute("User32.dll")]
    private static extern int FindWindow(String ClassName, String WindowName);

    //Import the SetForeground API to activate it
    [DllImportAttribute("User32.dll")]
    private static extern IntPtr SetForegroundWindow(int hWnd);


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Find the window, using the CORRECT Window Title, for example, Notepad
        int hWnd = FindWindow(null, "Untitled - Notepad");
        if (hWnd > 0) //If found
        {
            SetForegroundWindow(hWnd); //Activate it
        }
        else
        {
            MessageBox.Show("Window Not Found!");
        }


    }
}
}

泰,这样我就可以得到这个过程。。但是我怎样才能打开它呢?用任何“简单的方法”。你的方式很难理解,所以我可以了解过程。。但是我怎样才能打开它呢?用任何“简单的方法”。你的方式很难理解。和我对那个家伙说的一样:(谢天谢地,这个链接没有死,否则这个答案会死。)和我对那个家伙说的一样:(谢天谢地,这个链接没有死,否则这个答案会死。