static void Main()
        {
            //获取当前进程
            Process currentProcess = Process.GetCurrentProcess();  
            //获取当前运行程序完全限定名 
            string currentFileName = currentProcess.MainModule.FileName;
            //获取进程名为ProcessName的Process数组。 
            Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
            //遍历有相同进程名称正在运行的进程 
            foreach (Process process in processes)
            {
                if (process.MainModule.FileName == currentFileName)
                {
                    if (process.Id != currentProcess.Id) //根据进程ID排除当前进程 
                    {
                        //MessageBox.Show("系统已经运行!", "系统提示");                        
                        return;//返回已运行的进程实例                      
                     } 
                }
            }我做的程序被我隐藏到系统栏,我想就跟TT 浏览器的功能一样,隐藏之后如果忘记了,再次运行程序就激活程序。
可是 在 //MessageBox.Show("系统已经运行!", "系统提示");
 下面我不知道怎么写了,请给出代码。(再问一下我这种方法检查实例好吗?)

解决方案 »

  1.   

    FindWindow找到窗口句柄,然后ShowWindow显示出来
      

  2.   

    这个做法有很多 
    你可以通过SendMessageBox给进程发送消息. 但你的程序需要自己处理消息.另外你可以使用_Process.MainWindowHandle; 这个是窗体句柄.通过API来设置窗        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
      

  3.   

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
            public static extern bool SetForegroundWindow(IntPtr hWnd); 我在上面放的声明 然后在//MessageBox.Show("系统已经运行!", "系统提示"); 
    下面 打的
        SetForegroundWindow(currentProcess.MainWindowHandle); 
    但是不好用啊,程序没反应
      

  4.   

       Application.MessageBox('aa已经在运行了!', '提示信息', MB_OK);
       Wnd := FindWindow(nil, 'aa');
        ShowWindow(Wnd, SW_SHOW);
        ShowWindow(Wnd, SW_RESTORE);
        SetForegroundWindow(Wnd);
      

  5.   

    麻烦楼上能不能把代码给全一些
    很多 API 函数声明声明的
    谢谢了 
      

  6.   

    还没有试过C#API编程,大家推荐几本好点儿的书呢
      

  7.   

    你的这个功能是下面文章的一个功能子模块,带说明和完成代码和代码演示,请参考。
    基于.Net平台应用程序唯一运行实例实现
    http://blog.csdn.net/zhzuo/archive/2006/06/30/857405.aspx
    http://blog.csdn.net/zhzuo/archive/2006/07/04/874745.aspx