using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

namespace WindowsApplication1
{
    
static class Program
    
{
        
/// <summary>
        
/// 應用程式的主要進入點。
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            Process p 
= checkApp();
            
if (p == null)
            
{
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
false);
                Application.Run(
new Form1());
            }

            
else
            
{
                MessageBox.Show(
"該程式正在執行","友情提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }

        }


        
private static Process checkApp()
        
{
            
//獲取當前進程
            Process current = Process.GetCurrentProcess();
            
//通過進程名稱查找進程
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            
foreach (Process process in processes)
            
{
                
if (process.Id != current.Id)
                
{
                    
//確保例程從EXE文件執行
                    if (Assembly.GetExecutingAssembly().Location.Replace("/""\"== current.MainModule.FileName)
                    
{
                        
//返回另一個例程實例
                        return process;
                    }

                }

            }

            
return null;
        }

    }

}
 
posted on 2007-08-02 15:37  wkjs  阅读(162)  评论(0编辑  收藏  举报