这里写自定义目录标题
winform&&WPF只打开一个exe
winform:
在program.cs里面添加如下代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process process = GetCurrentInstance();
if (process != null)
{
if (IsIconic(process.MainWindowHandle))
{
ShowWindowAsync(process.MainWindowHandle, SW_RESTORE);
}
SetForegroundWindow(process.MainWindowHandle);
Application.Exit();
return;
}
SystemMgr.GetClass();
Application.Run(ProcessForm.GetProcessForm());
}
public const int SW_RESTORE = 9;
public static Process GetCurrentInstance(bool bIgnorePath = true)
{
Process currentProcess = Process.GetCurrentProcess();
Process result;
if (currentProcess.ProcessName.Contains("vshost"))
{
result = null;
}
else
{
Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in Processes)
{
if (process.Id != currentProcess.Id)
{
if (bIgnorePath)
{
return process;
}
bool b = Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName;
if (b)
{
return process;
}
}
}
result = null;
}
return result;
}
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
wpf:
在app.xaml.cs 里面添加如下代码
public App()
{
this.Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
Process process = GetCurrentInstance();
if (process != null)
{
if (IsIconic(process.MainWindowHandle))
{
ShowWindowAsync(process.MainWindowHandle, SW_RESTORE);
}
SetForegroundWindow(process.MainWindowHandle);
this.Shutdown();
return;
}
}
public const int SW_RESTORE = 9;
public Process GetCurrentInstance(bool bIgnorePath = true)
{
Process currentProcess = Process.GetCurrentProcess();
Process result;
if (currentProcess.ProcessName.Contains("vshost"))
{
result = null;
}
else
{
Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in Processes)
{
if (process.Id != currentProcess.Id)
{
if (bIgnorePath)
{
return process;
}
bool b = Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName;
if (b)
{
return process;
}
}
}
result = null;
}
return result;
}
[DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}