C# 设置程序启动项
程序员文章站
2022-12-29 08:14:23
托盘图标设置 新建一个NotifyIcon,会在托盘处显示一个图标。 NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标。 notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application. ......
托盘图标设置
新建一个notifyicon,会在托盘处显示一个图标。
notifyicon.icon可以直接设置一个ico图片,也可以延用原有程序的图标。
notifyicon.icon = system.drawing.icon.extractassociatedicon(application.executablepath);
1 public partial class mainwindow : window 2 { 3 private notifyicon notifyicon; 4 5 public mainwindow() 6 { 7 initializecomponent(); 8 setnotifyicon(); 9 this.hide(); 10 } 11 12 #region notifyicon 13 14 private void setnotifyicon() 15 { 16 this.notifyicon = new notifyicon(); 17 this.notifyicon.balloontiptext = "磁盘清理工具"; 18 this.notifyicon.showballoontip(2000); 19 this.notifyicon.text = "磁盘清理工具:每20天清理一次"; 20 this.notifyicon.icon = system.drawing.icon.extractassociatedicon(application.executablepath); 21 this.notifyicon.visible = true; 22 //打开菜单项 23 menuitem open = new menuitem("打开"); 24 open.click += new eventhandler(show); 25 //退出菜单项 26 menuitem exit = new menuitem("退出"); 27 exit.click += new eventhandler(close); 28 //关联托盘控件 29 menuitem[] childen = new menuitem[] { open, exit }; 30 notifyicon.contextmenu = new contextmenu(childen); 31 32 this.notifyicon.mousedoubleclick += new mouseeventhandler((o, e) => 33 { 34 if (e.button == mousebuttons.left) this.show(o, e); 35 }); 36 } 37 38 private void show(object sender, eventargs e) 39 { 40 this.visibility = visibility.visible; 41 this.showintaskbar = true; 42 this.activate(); 43 } 44 45 private void hide(object sender, eventargs e) 46 { 47 this.showintaskbar = false; 48 this.visibility = visibility.hidden; 49 } 50 51 private void close(object sender, eventargs e) 52 { 53 system.windows.application.current.shutdown(); 54 } 55 56 #endregion 57 58 #region 窗口 59 60 private void minimizebutton_onclick(object sender, routedeventargs e) 61 { 62 windowstate = windowstate.minimized; 63 } 64 65 private void closebutton_onclick(object sender, routedeventargs e) 66 { 67 this.hide(); 68 } 69 70 private void headergrid_onmouseleftbuttondown(object sender, mousebuttoneventargs e) 71 { 72 if (e.buttonstate == mousebuttonstate.pressed) 73 { 74 this.dragmove(); 75 } 76 } 77 78 #endregion 79 }
禁用多进程启动
1 //禁止双进程 2 bool cancreatenew; 3 using (system.threading.mutex m = new system.threading.mutex(true, system.windows.forms.application.productname, out cancreatenew)) 4 { 5 if (!cancreatenew) 6 { 7 this.shutdown(); 8 } 9 }
删除原有进程
1 /// <summary> 2 /// 删除原有进程 3 /// </summary> 4 /// <param name="processname"></param> 5 private void killprocess(string processname) 6 { 7 //得到所有打开的进程 8 try 9 { 10 process currentprocess = process.getcurrentprocess(); 11 var processes = process.getprocessesbyname(processname).where(process=> process.id!=currentprocess.id); 12 foreach (process thisproc in processes) 13 { 14 //找到程序进程,kill之。 15 if (!thisproc.closemainwindow()) 16 { 17 thisproc.kill(); 18 } 19 } 20 } 21 catch (exception ex) 22 { 23 24 } 25 }
设置开机自启动
1 private void setappautorun(bool autorun) 2 { 3 if (autorun) //设置开机自启动 4 { 6 string path = system.windows.forms.application.executablepath; 7 registrykey rk = registry.localmachine; 8 registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run"); 9 rk2.setvalue("jcshutdown", path); 10 rk2.close(); 11 rk.close(); 12 } 13 else //取消开机自启动 14 {
16 registrykey rk = registry.localmachine; 17 registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run"); 18 rk2.deletevalue("jcshutdown", false); 19 rk2.close(); 20 rk.close(); 21 } 22 }
app.cs中完整代码:
1 public partial class app : application 2 { 3 public app() 4 { 5 //禁止双进程 6 bool cancreatenew; 7 using (system.threading.mutex m = new system.threading.mutex(true, system.windows.forms.application.productname, out cancreatenew)) 8 { 9 if (!cancreatenew) 10 { 11 this.shutdown(); 12 } 13 } 14 15 setappautorun(true); 16 17 startup += app_startup; 18 } 19 20 private void setappautorun(bool autorun) 21 { 22 if (autorun) //设置开机自启动 23 { 24 messagebox.show("设置开机自启动,需要修改注册表", "提示"); // hovertree.com 25 string path = system.windows.forms.application.executablepath; 26 registrykey rk = registry.localmachine; 27 registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run"); 28 rk2.setvalue("jcshutdown", path); 29 rk2.close(); 30 rk.close(); 31 } 32 else //取消开机自启动 33 { 34 messagebox.show("取消开机自启动,需要修改注册表", "提示"); 35 registrykey rk = registry.localmachine; 36 registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run"); 37 rk2.deletevalue("jcshutdown", false); 38 rk2.close(); 39 rk.close(); 40 } 41 } 42 43 private void app_startup(object sender, startupeventargs e) 44 { 45 new autocleancachehelper(cleancacheveiwmodel.viewmodel).start(); 46 } 47 }