アプリケーションのインスタンスを1つ確保し、2つ目のインスタンスを開くときにフォーカスを設定するにはどうすればよいですか? アプリケーションのインスタンスを1つだけ強制実行するにはどうすればよいですか?
は、私が試した:これは、アプリケーションを集中されていないpublic partial class Form1 : Form {
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern
IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("USER32.DLL")]
public static extern
Boolean SetForegroundWindow(IntPtr hWnd);
private void Form1_Load(object sender, EventArgs e)
{
bool isRunning = Process.GetProcesses()
.Where(p => p.MainWindowTitle.Contains(Text))
.Count() > 1;
if (isRunning)
{
FocusWindow(Text);
Application.Exit();
}
}
public static void FocusWindow(string title)
{
SetForegroundWindow(FindWindow(null, title));
}
}
。これをどうすれば解決できますか?
ていますか? – Tigran
フォームを起動する前にProgram.csをチェックインするのは意味がありませんか? 'count> 0'でプロセス名のインスタンスを確認してください。 –
[C#でのシングルインスタンスアプリケーション](http://blogs.msdn.com/b/tyler_whitney/archive/2005/11/28/497604.aspx) – LarsTech