Il codice che devi usare è quello dell'ultimo post, non serve Process.ProcessName. In C# corrisponde a questo:
[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int SetForegroundWindow(IntPtr handle);
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
private void TuaForm_Load(object sender, EventArgs e)
{
//Prova ad aprire il mutex
Mutex mut = null;
try
{
mut = Mutex.OpenExisting("SINGIST");
}
catch (Exception)
{
}
//Se non è aperto significa che non esiste, quindi lo crea
if (mut == null)
{
mut = new Mutex(true, "SINGIST");
}
//altrimenti indica alla finestra che sta venendo chiusa e cerca
//la finestra da portare in primo piano
else
{
this.Text = this.Text + "_Closing";
EnumWindows(new EnumWindowProc(this.OpenInstance), IntPtr.Zero);
}
}
private bool OpenInstance(IntPtr hwnd, IntPtr lParam)
{
//Se la finestra è la stessa corrente non è quella da portare in primo piano
if (hwnd == this.Handle)
{
return true;
}
//Ottiene il nome della finestra elaborata
StringBuilder sb = new StringBuilder();
GetWindowText(hwnd, sb, this.Text.Length);
//Se la finestra ha come testo lo stesso di quella corrente
//la porta in primo piano e chiude l'applicazione corrente
if (sb.Length > 0 && this.Text.StartsWith(sb.ToString()))
{
SetForegroundWindow(hwnd);
Process.GetCurrentProcess().Kill();
return false;
}
//Altrimenti cerca la finestra successiva
else
{
return true;
}
}
Luca