托盘图标设置

新建一个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# 设置程序启动项的更多相关文章

  1. 源码编译安装nginx及设置开机启动项

    1.上传nginx文档:解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ...

  2. BIOS设置第一启动项

    在电脑的Bois中怎样去设置第一启动项.. 对于很多新手朋友来说,BIOS满屏英文,生涩难懂,话说我原来也很排斥BIOS,界面太丑,光看界面就没什么兴趣,更谈不上深入研究,大多数人在电脑城装机的时候都 ...

  3. linux 设置开机启动项两种方式

    原文链接:http://blog.csdn.net/karchar/article/details/52489572 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务. 在解问题之前 ...

  4. 02-4设置第一启动项--U盘装系统中bios怎么设置USB启动

    整个U盘启动里最关键的一步就是设置U盘启动了,本教程内只是以特定型号的电脑为例进行演示,鉴于各种电脑不同BIOS设置U盘启动各有差异,所以如果下面的演示不能适用于你的电脑,建议去百度或者谷歌搜索一下你 ...

  5. windows设置开机启动项

    一.windows下设置开机启动有如下方法 1 注册表启动项目RUN 2 计划任务,在"windows管理">"计划任务管理器"中新建任务,在操作栏指定要 ...

  6. win7如何快速设置开机启动项?

    添加开机启动项方法: 找到windows开始菜单->所有程序->启动,右键打开, 进入C:\Users\Ocean\AppData\Roaming\Microsoft\Windows\St ...

  7. Linux设置开机启动项

    第一种方式:ln -s 建立启动软连接 在Linux中有7种运行级别(可在/etc/inittab文件设置),每种运行级别分别对应着/etc/rc.d/rc[0~6].d这7个目录 Tips:/etc ...

  8. 02-3设置第一启动项--进入BIOS设置USB方式启动

    设置USB方式启动 https://zhinan.sogou.com/guide/detail/?id=1610014869 如何设置电脑从U盘启动呢?今天小编教大家如何进入BIOS设置USB方式启动 ...

  9. 02-2设置第一启动项--进入Bios界面设置U盘为第一启动项

    进入Bios界面设置U盘为第一启动项: 开机,当电脑处于启动状态,屏幕显示电脑LOGO时,按下F2键.(根据电脑的不同,进入BIOS的功能键也不同,可根据自己电脑的型号百度搜索相关功能键) 按电脑方向 ...

随机推荐

  1. jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决

    最近开始复习以前学过的JDBC今天肝了一晚上 来睡睡回笼觉,长话短说 我们现在开始. 我们先写一个获取数据库连接的jdbc封装类 以后可以用 如果不是maven环境的话在src文件下新建一个db.pr ...

  2. java happens-before原则规则

    程序次序规则:一个线程内,按照代码顺序,书写在前面的操作先行发生于书写在后面的操作: 锁定规则:一个unLock操作先行发生于后面对同一个锁额lock操作: volatile变量规则:对一个变量的写操 ...

  3. Python-常用 Linux 命令的基本使用

    常用 Linux 命令的基本使用 操作系统 作用:管理好硬件设备,让软件可以和硬件发生交互类型 桌面操作系统 Windows macos linux 服务器操作系统 linux Windows ser ...

  4. 从壹开始 [vueAdmin后台] 之三 || 动态路由配置 & 项目快速开发

    回顾 今天VS 2019正式发布,实验一波,你安装了么?Blog.Core 预计今天会升级到 Core 3.0 版本. 哈喽大家周三好!本来今天呢要写 Id4 了,但是写到了一半,突然有人问到了关于 ...

  5. Asp.Net Core中HttpClient的使用方式

    在.Net Core应用开发中,调用第三方接口也是常有的事情,HttpClient使用人数.使用频率算是最高的一种了,在.Net Core中,HttpClient的使用方式随着版本的升级也发生了一些变 ...

  6. 安全性测试入门:DVWA系列研究(二):Command Injection命令行注入攻击和防御

    本篇继续对于安全性测试话题,结合DVWA进行研习. Command Injection:命令注入攻击. 1. Command Injection命令注入 命令注入是通过在应用中执行宿主操作系统的命令, ...

  7. d3实现家族树

      1.  jQuery和CSS3支持移动手机的DOM元素移动和缩放插件:panzoom   2.拖动:jqueryUI-Draggable.touchpunch   3.图表:echart.heig ...

  8. .net mvc + layui做图片上传(一)

    图片上传和展示是互联网应用中比较常见的一个功能,最近做的一个门户网站项目就有多个需要上传图片的功能模块.关于这部分内容,本来功能不复杂,但后面做起来却还是出现了一些波折.因为缺乏经验,对几种图片上传的 ...

  9. C# 替换Word文本—— 用文档、图片、表格替换文本

    编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改.在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文 ...

  10. 微信小程序echarts层级太高

    项目中因为需求,底部的tab导航栏是自己写的,在开发者工具中一切正常:但是在真机上页面滑动时,echarts的层级比tab高,调过两者的z-index后仍然如此. 经过查找后发现cover-view和 ...