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的功能键也不同,可根据自己电脑的型号百度搜索相关功能键) 按电脑方向 ...
随机推荐
- 在Eclipse上Maven环境配置使用
1. 安装配置Maven: 1.1 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven. Maven下载地址: http://maven. ...
- vue+axios访问本地json数据踩坑点
当我们想在vue项目中模拟后台接口访问json数据时,我们发现无论如何也访问不到本地的json数据. 注意:1.在vue-cli项目中,我们静态资源只能放在static文件夹中,axios使用get请 ...
- Django:Python3.6.2+Django2.0配置MySQL
持续学习Django中... Django默认使用的数据库是python自带的SQLlite3,但SQLlite并不适用于大型的项目,因此我将数据库换成了MySQL,下面介绍下Django如何配置数据 ...
- 【STM32H7教程】第7章 STM32H7下载和调试方法(IAR8)
完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第7章 STM32H7下载和调试方法(IAR8) 本 ...
- 为自己搭建一个分布式 IM(即时通讯) 系统
前言 大家新年快乐! 新的一年第一篇技术文章希望开个好头,所以元旦三天我也没怎么闲着,希望给大家带来一篇比较感兴趣的干货内容. 老读者应该还记得我在去年国庆节前分享过一篇<设计一个百万级的消息推 ...
- 为什么range不是迭代器?range到底是什么类型?
迭代器是 23 种设计模式中最常用的一种(之一),在 Python 中随处可见它的身影,我们经常用到它,但是却不一定意识到它的存在.在关于迭代器的系列文章中(链接见文末),我至少提到了 23 种生成迭 ...
- Deepin linux Compass.app安装
compass.app是集成了sass的工具,安装完Compass就能够使用sass. 首先,上官网 可以看到官网上推荐的两种sass使用方式,application&command line ...
- 用css实现正方形div
目标:实现一个正方形,这个正方形边长等于 方法一:使用单位vw, (ps我觉得这个是最简单的方法) html结构也很简单,只有一个div即可 <html> <body> < ...
- Odoo开源智造IT经理人创业圆梦计划正式启动
概念定义 IT经理人创业圆梦计划是什么? 甲方IT经理人的行业背景 + 其他甲方需求及可靠信任的线索资源 = 自主创业圆梦计划 具体措施 甲方IT经理人的职业行业背景取得其他甲方需求线索及信任——通过 ...
- Unable to get the CMake version located at
出现这个问题说明你没有安装CMake,这个是使用NDK的时候需要下载的,可以在as上点击下载, SDK Tool里面