我现在可以通过进程判断,判断出是否有我想启动的应用程序的的进程存在,然后如果我查到有此进程存在那么我就想让这个进程,也就是应用程序把主窗体激活显示出来给用户看。类似迅雷。
比如如果你启动了一个迅雷,然后下在东西,把迅雷最小化了,然后又双击迅雷想在打开一个,这个时候迅雷窗口就会自动显示出来,这样就避免反复启动应用程序。请高手帮助!

解决方案 »

  1.   

    // 用于激活已打开的窗体
    [DllImport("user32.dll")]
    public static extern void SetForegroundWindow(IntPtr hwnd); // 用于获取当前激活的窗体句柄
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow(); /// <summary>
    /// 功能:激活已打开的登录窗体
    /// </summary>
    /// <res>
    /// 实现流程
    /// 1. 获取当前已运行的所有DMS进程
    /// 2. 遍历所获取进程
    /// 3. 判断:是否有进程的MainWindowTitle为"登录"
    /// 3.1 如果有则调用API:SetForegroundWindow激活该进程的主窗体
    /// 3.2 返回true
    /// 4. 否则返回false
    /// </res>
    /// <returns>true:已经有打开的登录进程 false: 没有已经打开的登录进程</returns>
    public static bool ActiveiseLoginWindow()
    {
    Process [] proc = Process.GetProcessesByName("YourProcessName"); foreach(Process p in proc)
    {
    SetForegroundWindow(p.MainWindowHandle);
    return true;
    }

    return false;
    }
      

  2.   

    Edifier0709(腦袋重構中.....) 推荐该兄的方法, 我也是这么做的
      

  3.   

    C#里面本来就有一个防止程序重复运行的类:Mutex
    你先引用空间:using System.Threading
    然后在void Main()
    {
    bool isCreat;
    Mutex mutex = new Mutex(true,"XXXX",out isCreat);   
    if(isCreat)
    {
       Application.Run(new Form1());
       mutex.ReleaseMutex();
    }
    } //XXXX为自定义字符串,你可以写该程序的目录路径都可以
      

  4.   

    //愚公说的
    using System.Diagnostics;
    using System.Reflection;
     public static Process RunningInstance()         {             Process current = Process.GetCurrentProcess();             Process[] processes = Process.GetProcessesByName(current.ProcessName);              //Loop through the running processes in with the same name             foreach (Process process in processes)             {                 //Ignore the current process                 if (process.Id != current.Id)                 {                     //Make sure that the process is running from the exe file.                     if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)                     {                         //Return the other process instance.                         return process;                     }                 }             }              //No other instance was found, return null.             return null;         } 
      

  5.   


    C#里面本来就有一个防止程序重复运行的类:Mutex
    你先引用空间:using System.Threading
    然后在void Main()
    {
    bool isCreat;
    Mutex mutex = new Mutex(true,"XXXX",out isCreat);   
    if(isCreat)
    {
       Application.Run(new Form1());
       mutex.ReleaseMutex();
    }
    } //XXXX为自定义字符串,你可以写该程序的目录路径都可以----------------------------
     这个正解!