C#程序启动项的设置方法
本文为大家分享了C#程序启动项的设置方法,供大家参考,具体内容如下
托盘图标设置
新建一个NotifyIcon,会在托盘处显示一个图标。
NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标。
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
public partial class MainWindow : Window { private NotifyIcon notifyIcon; public MainWindow() { InitializeComponent(); SetNotifyIcon(); this.Hide(); } #region NotifyIcon private void SetNotifyIcon() { this.notifyIcon = new NotifyIcon(); this.notifyIcon.BalloonTipText = "磁盘清理工具"; this.notifyIcon.ShowBalloonTip(2000); this.notifyIcon.Text = "磁盘清理工具:每20天清理一次"; this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath); this.notifyIcon.Visible = true; //打开菜单项 MenuItem open = new MenuItem("打开"); open.Click += new EventHandler(Show); //退出菜单项 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) this.Show(o, e); }); } private void Show(object sender, EventArgs e) { this.Visibility = Visibility.Visible; this.ShowInTaskbar = true; this.Activate(); } private void Hide(object sender, EventArgs e) { this.ShowInTaskbar = false; this.Visibility = Visibility.Hidden; } private void Close(object sender, EventArgs e) { System.Windows.Application.Current.Shutdown(); } #endregion #region 窗口 private void MinimizeButton_OnClick(object sender, RoutedEventArgs e) { WindowState = WindowState.Minimized; } private void CloseButton_OnClick(object sender, RoutedEventArgs e) { this.Hide(); } private void HeaderGrid_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ButtonState == MouseButtonState.Pressed) { this.DragMove(); } } #endregion }
禁用多进程启动
//禁止双进程 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) { //找到程序进程,kill之。 if (!thisproc.CloseMainWindow()) { thisproc.Kill(); } } } catch (Exception ex) { } }
设置开机自启动
private void SetAppAutoRun(bool autoRun) { if (autoRun) //设置开机自启动 { string path = System.Windows.Forms.Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.SetValue("JcShutdown", path); rk2.Close(); rk.Close(); } else //取消开机自启动 { RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("JcShutdown", false); rk2.Close(); rk.Close(); } }
App.cs中完整代码:
public partial class App : Application { public App() { //禁止双进程 bool canCreateNew; using (System.Threading.Mutex m = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out canCreateNew)) { if (!canCreateNew) { this.Shutdown(); } } SetAppAutoRun(true); Startup += App_Startup; } private void SetAppAutoRun(bool autoRun) { if (autoRun) //设置开机自启动 { MessageBox.Show("设置开机自启动,需要修改注册表", "提示"); // hovertree.com string path = System.Windows.Forms.Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.SetValue("JcShutdown", path); rk2.Close(); rk.Close(); } else //取消开机自启动 { MessageBox.Show("取消开机自启动,需要修改注册表", "提示"); RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("JcShutdown", false); rk2.Close(); rk.Close(); } } private void App_Startup(object sender, StartupEventArgs e) { new AutoCleanCacheHelper(CleanCacheVeiwModel.ViewModel).Start(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:C#中String和StringBuilder的简介与区别
栏 目:C#教程
下一篇:c#中的扩展方法学习笔记
本文标题:C#程序启动项的设置方法
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5010.html
您可能感兴趣的文章
- 01-10.net2.0+ Winform项目实现弹出容器层
- 01-10WinForm实现程序一段时间不运行自动关闭的方法
- 01-10C#实现将程序锁定到Win7任务栏的方法
- 01-10C#使用windows服务开启应用程序的方法
- 01-10C#一个简单的定时小程序实现代码
- 01-10C#实现在启动目录创建快捷方式的方法
- 01-10C#实现程序等待延迟执行的方法
- 01-10C#使用Mutex简单实现程序单实例运行的方法
- 01-10C#计算程序执行过程花费时间的方法
- 01-10C#实现将应用程序设置为开机启动的方法


阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax实现页面的局部加载
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 08-05dedecms(织梦)副栏目数量限制代码修改