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的功能键也不同,可根据自己电脑的型号百度搜索相关功能键) 按电脑方向 ...
随机推荐
- ./configure、make、make install
这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤 一.基本信息 1../configure 是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不 ...
- ServletContextListener
在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...
- JVM学习记录-类加载的过程
类的整个生命周期的7个阶段是:加载(Loading).验证(Verification).准备(Preparation).解析(Resolution).初始化(Initialization).使用(Us ...
- 利用策略模式优化过多 if else 代码
前言 不出意外,这应该是年前最后一次分享,本次来一点实际开发中会用到的小技巧. 比如平时大家是否都会写类似这样的代码: if(a){ //dosomething }else if(b){ //dosh ...
- 一个实时收集MySql变更记录的组件CanalSharp.AspNetCore
一.关于CanalSharp CanalSharp 是阿里巴巴开源项目 Canal 的 .NET 客户端.为 .NET 开发者提供一个更友好的使用 Canal 的方式.Canal 是mysql数据库b ...
- Python 转化成 PB 格式数据
一.概述 Protocol Buffers 是 Google 公司开发的一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯 ...
- Asp.Net Core&钉钉开发系列
阿里钉钉在商业领域的规模越来越大,基于钉钉办公的企业越来越多,将一个企业内现有用到的工具(如钉钉)能够更融入到他们的工作中,提高工作效率,那便需要开发者不断的学习.应用了,同时,个人也有一个预感,未来 ...
- 10 年三线小城 IT 开发的感悟
一贯都是写技术博客,从来没写过感悟类文章,因为文笔不好.今天看到了大飞的一篇文章,<技术人,请不要封闭自己>,真的感触太深了. 一 先说说我自己,我并非科班出身,大学毕业后一直没找到好的工 ...
- 卷积神经网络之AlexNet
由于受到计算机性能的影响,虽然LeNet在图像分类中取得了较好的成绩,但是并没有引起很多的关注. 知道2012年,Alex等人提出的AlexNet网络在ImageNet大赛上以远超第二名的成绩夺冠,卷 ...
- iOS----------学习路线思维导图
UI相关 Runtime OC特性 内存管理 Block 多线程 Runloop 网络相关 设计模式 架构 算法 第三方库