C# .net 只运行一个实例及系统自动启动辅助类
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace mons
{
/// <summary>
/// 只运行一个实例及系统自动启动辅助类
/// </summary>
public class StartupHelper
{
#region 设置软件自动启动
static RegistryKey registryKeyApp =
Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" true);
/// <summary>
/// 软件是否设置系统自动启动
/// </summary>
/// <param name="app">软件名称</param>
/// <returns></returns>
public static bool WillRunAtStartup(string app)
{
try
{
return string.Equals(registryKeyApp.GetValue(app) mandLine);
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 系统设置/取消自动启动
/// </summary>
/// <param name="app">软件名称</param>
/// <param name="shouldRun">设置/取消自动启动</param>
public static void RunAtStartup(string app bool shouldRun)
{
RunAtStartup(app shouldRun mandLine);
}
/// <summary>
/// 系统设置/取消自动启动
/// </summary>
/// <param name="app">软件名称</param>
/// <param name="shouldRun">是否设置</param>
/// <param name="exePath">系统执行路径(可增加配置参数)</param> public static void RunAtStartup(string app bool shouldRun string exePath) {
try
{
if (shouldRun)
{
registryKeyApp.SetValue(app exePath);
}
else
{
registryKeyApp.DeleteValue(app false);
}
}
catch (Exception ex)
{
Trace.WriteLine("Unable to RunAtStartup: " + ex);
}
}
#endregion
#region 只运行一个实例
/// <summary>
/// 获取软件运行的系统进程对象
/// Process instance = StartupHelper.RunningInstance();
/// if (instance == null)
/// {
/// //正常启动的代码
/// }
/// else
/// {
/// StartupHelper.HandleRunningInstance(instance);
/// }
/// </summary>
/// <returns></returns>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
return process;
}
}
return null;
}
/// <summary>
/// 处理重复运行的事件
/// </summary>
/// <param name="instance">系统进程对象</param>
/// <param name="message">提示消息</param>
public static void HandleRunningInstance(Process instance)
{
HandleRunningInstance(instance null);
}
/// <summary>
/// 处理重复运行的事件
/// </summary>
/// <param name="instance">系统进程对象</param>
/// <param name="message">提示消息</param>
public static void HandleRunningInstance(Process instance string message)
{
if (!string.IsNullOrEmpty(message))
{
MessageUtil.ShowWarning(message);
}
ShowWindowAsync(instance.MainWindowHandle WS_SHOWNORMAL); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}