C# 设置程序启动项
托盘图标设置
新建一个NotifyIcon,会在托盘处显示一个图标。
NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标。
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
private NotifyIcon _notifyIcon;
private void SetNotifyIcon()
{
this._notifyIcon = new NotifyIcon();
this._notifyIcon.BalloonTipText = "翻译小工具";
this._notifyIcon.ShowBalloonTip();
this._notifyIcon.Text = "集成金山、有道非官方数据的翻译工具";
this._notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this._notifyIcon.Visible = true;
//打开菜单项
MenuItem open = new MenuItem("打开");
open.Click += new EventHandler(ShowMainWindow);
//退出菜单项
MenuItem exit = new MenuItem("退出");
exit.Click += new EventHandler(Close);
//关联托盘控件
MenuItem[] childen = new MenuItem[] { open, exit };
_notifyIcon.ContextMenu = new ContextMenu(childen); this._notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) =>
{
if (e.Button == MouseButtons.Left) ShowMainWindow(o, e);
});
} private void ShowMainWindow(object sender, EventArgs e)
{
_mainWindow.Visibility = Visibility.Visible;
_mainWindow.ShowInTaskbar = true;
_mainWindow.Activate();
} private void Close(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
禁用多进程启动
//禁止双进程
bool canCreateNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out canCreateNew))
{
if (!canCreateNew)
{
this.Shutdown();
}
}
删除原有进程
/// <summary>
/// 删除原有进程
/// </summary>
/// <param name="processName"></param>
private void KillProcess(string processName)
{
try
{
//删除所有同名进程
Process currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(processName).Where(process => process.Id != currentProcess.Id);
foreach (Process thisproc in processes)
{
thisproc.Kill();
}
}
catch (Exception ex)
{
}
}
设置开机自启动
关于C#开机自动启动程序的方法,修改注册表:
1. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
2.HKEY_Current_User\Software\Microsoft\Windows\CurrentVersion\Run
系统有默认选择注册表位置。如果LocalMachine设置异常,则可以在CurrentUser中设置开机启动项。
当然通过管理员权限,也是可以在LocalMachine设置的。
private void SetAppAutoRun(bool autoRun)
{
try
{
string executablePath = System.Windows.Forms.Application.ExecutablePath;
string exeName = Path.GetFileNameWithoutExtension(executablePath);
SetAutoRun(autoRun, exeName, executablePath);
}
catch (Exception e)
{
}
}
private bool SetAutoRun(bool autoRun, string exeName, string executablePath)
{
bool success = SetAutoRun(Registry.LocalMachine, autoRun, exeName, executablePath);
if (!success)
{
success = SetAutoRun(Registry.CurrentUser, autoRun, exeName, executablePath);
}
return success;
}
private bool SetAutoRun(RegistryKey rootKey, bool autoRun, string exeName, string executablePath)
{
try
{
RegistryKey autoRunKey = rootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (autoRunKey == null)
{
autoRunKey = rootKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
}
if (autoRunKey != null)
{
if (autoRun) //设置开机自启动
{
autoRunKey.SetValue(exeName, $"\"{executablePath}\" /background");
}
else //取消开机自启动
{
autoRunKey.DeleteValue(exeName, false);
}
autoRunKey.Close();
autoRunKey.Dispose();
}
}
catch (Exception e)
{
rootKey.Close();
rootKey.Dispose();
return false;
}
rootKey.Close();
rootKey.Dispose();
return true;
}
App.cs中完整代码:
public partial class App : Application
{
MainWindow _mainWindow;
public App()
{
KillProcess(System.Windows.Forms.Application.ProductName); SetAppAutoRun(true); Startup += App_Startup;
} private void App_Startup(object sender, StartupEventArgs e)
{
_mainWindow = new MainWindow();
SetNotifyIcon();
} #region 托盘图标 private NotifyIcon _notifyIcon;
private void SetNotifyIcon()
{
this._notifyIcon = new NotifyIcon();
this._notifyIcon.BalloonTipText = "翻译小工具";
this._notifyIcon.ShowBalloonTip();
this._notifyIcon.Text = "集成金山、有道非官方数据的翻译工具";
this._notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this._notifyIcon.Visible = true;
//打开菜单项
MenuItem open = new MenuItem("打开");
open.Click += new EventHandler(ShowMainWindow);
//退出菜单项
MenuItem exit = new MenuItem("退出");
exit.Click += new EventHandler(Close);
//关联托盘控件
MenuItem[] childen = new MenuItem[] { open, exit };
_notifyIcon.ContextMenu = new ContextMenu(childen); this._notifyIcon.MouseDoubleClick += new MouseEventHandler((o, e) =>
{
if (e.Button == MouseButtons.Left) ShowMainWindow(o, e);
});
} private void ShowMainWindow(object sender, EventArgs e)
{
_mainWindow.Visibility = Visibility.Visible;
_mainWindow.ShowInTaskbar = true;
_mainWindow.Activate();
} private void Close(object sender, EventArgs e)
{
Application.Current.Shutdown();
} #endregion #region 删除原有进程 /// <summary>
/// 删除原有进程
/// </summary>
/// <param name="processName"></param>
private void KillProcess(string processName)
{
try
{
//删除所有同名进程
Process currentProcess = Process.GetCurrentProcess();
var processes = Process.GetProcessesByName(processName).Where(process => process.Id != currentProcess.Id);
foreach (Process thisproc in processes)
{
thisproc.Kill();
}
}
catch (Exception ex)
{
}
} #endregion #region 开机自启动 private void SetAppAutoRun(bool autoRun)
{
try
{
string executablePath = System.Windows.Forms.Application.ExecutablePath;
string exeName = Path.GetFileNameWithoutExtension(executablePath);
SetAutoRun(autoRun, exeName, executablePath);
}
catch (Exception e)
{
}
}
private bool SetAutoRun(bool autoRun, string exeName, string executablePath)
{
bool success = SetAutoRun(Registry.CurrentUser, autoRun, exeName, executablePath);
if (!success)
{
success = SetAutoRun(Registry.LocalMachine, autoRun, exeName, executablePath);
}
return success;
}
private bool SetAutoRun(RegistryKey rootKey, bool autoRun, string exeName, string executablePath)
{
try
{
RegistryKey autoRunKey = rootKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (autoRunKey == null)
{
autoRunKey = rootKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
}
if (autoRunKey != null)
{
if (autoRun) //设置开机自启动
{
autoRunKey.SetValue(exeName, $"\"{executablePath}\" /background");
}
else //取消开机自启动
{
autoRunKey.DeleteValue(exeName, false);
}
autoRunKey.Close();
autoRunKey.Dispose();
}
}
catch (Exception e)
{
rootKey.Close();
rootKey.Dispose();
return false;
}
rootKey.Close();
rootKey.Dispose();
return true;
} #endregion
}
C# 设置程序启动项的更多相关文章
- 源码编译安装nginx及设置开机启动项
1.上传nginx文档:解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ...
- BIOS设置第一启动项
在电脑的Bois中怎样去设置第一启动项.. 对于很多新手朋友来说,BIOS满屏英文,生涩难懂,话说我原来也很排斥BIOS,界面太丑,光看界面就没什么兴趣,更谈不上深入研究,大多数人在电脑城装机的时候都 ...
- linux 设置开机启动项两种方式
原文链接:http://blog.csdn.net/karchar/article/details/52489572 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务. 在解问题之前 ...
- 02-4设置第一启动项--U盘装系统中bios怎么设置USB启动
整个U盘启动里最关键的一步就是设置U盘启动了,本教程内只是以特定型号的电脑为例进行演示,鉴于各种电脑不同BIOS设置U盘启动各有差异,所以如果下面的演示不能适用于你的电脑,建议去百度或者谷歌搜索一下你 ...
- windows设置开机启动项
一.windows下设置开机启动有如下方法 1 注册表启动项目RUN 2 计划任务,在"windows管理">"计划任务管理器"中新建任务,在操作栏指定要 ...
- win7如何快速设置开机启动项?
添加开机启动项方法: 找到windows开始菜单->所有程序->启动,右键打开, 进入C:\Users\Ocean\AppData\Roaming\Microsoft\Windows\St ...
- Linux设置开机启动项
第一种方式:ln -s 建立启动软连接 在Linux中有7种运行级别(可在/etc/inittab文件设置),每种运行级别分别对应着/etc/rc.d/rc[0~6].d这7个目录 Tips:/etc ...
- 02-3设置第一启动项--进入BIOS设置USB方式启动
设置USB方式启动 https://zhinan.sogou.com/guide/detail/?id=1610014869 如何设置电脑从U盘启动呢?今天小编教大家如何进入BIOS设置USB方式启动 ...
- 02-2设置第一启动项--进入Bios界面设置U盘为第一启动项
进入Bios界面设置U盘为第一启动项: 开机,当电脑处于启动状态,屏幕显示电脑LOGO时,按下F2键.(根据电脑的不同,进入BIOS的功能键也不同,可根据自己电脑的型号百度搜索相关功能键) 按电脑方向 ...
随机推荐
- i春秋——Misc之百度杯
今天心里很是不开森,想想往日何必那么努力呢?不如你的比比皆是,可是人家就是因为有关系,你又能怎样呢? 你所有应该有的都被打翻了,别灰心,至少你曾经努力过! 愿我未来的学弟学妹们都能一直开开心心的过好每 ...
- ruby整理
参考博客:https://www.cnblogs.com/felixzh/p/8081622.html 官网地址 https://rvm.io/rvm/install 一.前提 centos6.8下 ...
- java自动化-数据驱动juint演示,上篇
本文旨在帮助读者介绍,一般的全自动化代码接口,并简单介绍如何使用数据驱动来实现简单的自动化 在经过上述几个博客介绍后,相信读者对自动启动执行一个java编译过的class有了一定了解,也完全有能力去执 ...
- 使用CompletableFuture实现异步编程
在开发中会碰到一种场景,如下 Object result1 = service1.func1();//执行80ms Object result2 =service2.func2();//执行50ms ...
- BeetleX和Asp.net Core之webapi基础性能对比
本文主要针对BeetleX和Asp.net Core在基础WebApi功能性能对比 测试环境描述 硬件配置:E1230V2 16G内存 10Gb带宽 操作系统:Windows server 2008 ...
- 深度学习之卷积神经网络(CNN)的应用-验证码的生成与识别
验证码的生成与识别 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10755361.html 目录 1.验证码的制 ...
- Unity导航 (寻路系统Nav Mesh Agent)
第一种 简单寻路 地面接触到的.到达目标点不用跳跃能够一直走路到达.场景视图中简单搭设几个物体.胶囊体为寻路者,黄球为目标点 红地板,绿色障碍物.现将地板以及障碍物选中 在检视面板设置静态为Navig ...
- 简单导出下载excel的方法
简单导出excel方法 /// <summary> /// Excel打包下载 /// </summary> /// <returns></returns&g ...
- Windows Server 2016 安装虚拟机版黑群晖
硬件配置 Dell R730 CPU: Intel(R) Xeon(R) CPU E5-2603 v4 @1.70GHz(6 cores) Ram: 16Gb HDD: 系统-600GB SAS X2 ...
- svn版本控制迁移到git
获得原 SVN 仓库使用的作者名字列表 因为导入到git需要配置原作者(svn提交人)和git账户的映射关系 其格式为: vim authors-transform.txt taoxs = xsTao ...