string prfun(){
bool i=false;
process pName=Process.Start("ex.exe");
foreach (Process otherProc in 
Process.GetProcessByName(pName.ProcessName))
{
if (pName.ProcessName== .ProcessName)
  i=true;
 if(i)
 otherProc.Kill();
}
}

解决方案 »

  1.   

    string prfun(){
    bool i=false;
    process pName=Process.Start("ex.exe");
    foreach (Process otherProc in 
    Process.GetProcessByName(pName.ProcessName))
    {
    if (pName.ProcessName==otherProc .ProcessName)
      i=true;
     if(i)
     otherProc.Kill();
    }
    }
      

  2.   

    prfun(){
    bool i=false;
    process pName=Process.Start("ex.exe");
    foreach (Process otherProc in 
    Process.GetProcessByName(pName.ProcessName))
    {
    if (pName.ProcessName==otherProc .ProcessName)
      i=true;
     if(i)
     otherProc.Kill();
    }
    }
      

  3.   

    斑竹,你把第一个实例也杀掉了了?应该判断processID不同,而processName相同的就kill。想把已经启动的外部程序放到顶层,必须用win32。
      

  4.   

    调用API FindWindowEx(HWND hWndParent, HWND hWndNext, /*in*/LPCTSTR
    szClassName, /*in*/LPCTSTR szWindowTitle)方法.
    比较有用的是这句,我们可以使用registered window class name来找到所要窗口.
    如: IE窗口(IEFrame是所有打开的IE的标识).protected void FindPopup()

    IntPtr hParent = IntPtr.Zero; 
    IntPtr hNext = IntPtr.Zero; 
    String sClassNameFilter = "IEFrame"; // 所有IE窗口的
    类 
    do 

    hNext =
    NativeWIN32.FindWindowEx(hParent,hNext,sClassNameFilter,IntPtr.Zero);
    // we've got a hwnd to play with if ( !hNext.Equals(IntPtr.Zero) )
    { // get window caption NativeWIN32.STRINGBUFFER
    sLimitedLengthWindowTitle; NativeWIN32.GetWindowText(hNext, out
    sLimitedLengthWindowTitle, 256); String sWindowTitle =
    sLimitedLengthWindowTitle.szText; if (sWindowTitle.Length>0)
    { // find this caption in the list of banned captions
    foreach (ListViewItem item in listView1.Items)
    { if ( sWindowTitle.StartsWith(item.Text) )
    NativeWIN32.SendMessage(hNext, NativeWIN32.WM_SYSCOMMAND,
    NativeWIN32.SC_CLOSE,
    IntPtr.Zero); // try soft kill } } } }
    while (!hNext.Equals(IntPtr.Zero)); } public class NativeWIN32{
    [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern
    IntPtr FindWindowEx(IntPtr parent /*HWND*/,
    IntPtr next /*HWND*/, string
    sClassName, IntPtr
    sWindowTitle); }
      

  5.   

    要睡觉了,还是你自己整理吧,代码都在我的这篇文章中,看看就知道了. :)弹出窗口杀手
    http://www.csdn.net/develop/read_article.asp?id=15534
    http://www.csdn.net/develop/read_article.asp?id=15535
      

  6.   

    请看我这段代码,能满足你的要求,并附上详细解释。
    必须感谢TheAres 曾给我的指导。
    引用
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Reflection; 其他代码省略。仅仅从一个form启动讲。每个form都有个main入口。
    static void Main() 
    {  Process instance = RunningInstance(); //获取运行中的实例
       if (instance==null)
         Application.Run(new Form1());
       else
       {
         HandleRunningInstance(instance);//处理得到的进程。
        }
    }
       private static Process RunningInstance()
       {Process current = Process.GetCurrentProcess(); //得到当前form1的进程
       Process[] processes = Process.GetProcessesByName(current.ProcessName);// 得到所有同名进程。
    foreach(Process MyProcess in processes)
    {
    if(MyProcess.Id!= current.Id) //不同的进程id,也就是肯定不是同一进程
    {
    if(Assembly.GetExecutingAssembly().Location.Replace("/","\\")==current.MainModule.FileName) //进程名不同而id不同 的时候,比较程序启动的路径
    return MyProcess;

    }
    }
    return null;
    }
    private static void HandleRunningInstance(Process instance)
    {
    MessageBox.Show("程序已经运行!");
    ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口
    SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
                 }
    [DllImport("User32.dll")]
    private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
    [DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(System.IntPtr hWnd);