C#开机启动,托盘图标等小技巧

时光总嘲笑我的痴心妄想 提交于 2020-08-18 08:20:50

1. 开机启动

  需要引用第三方库, Windows Script Host Object Model

1 using IWshRuntimeLibrary;
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
  1     // https://www.cnblogs.com/mq0036/p/12117955.html
  2     class KFSystemAutoStart
  3     {
  4         /// <summary>
  5         /// 快捷方式名称
  6         /// </summary>
  7         private const string QuickName = "销售系统工具";
  8         /// <summary>
  9         /// 获取系统自启动目录
 10         /// </summary>
 11         private string systemStartPath
 12         {
 13             get
 14             {
 15                 return Environment.GetFolderPath(Environment.SpecialFolder.Startup);
 16             }
 17         }
 18         /// <summary>
 19         /// 程序完整路径
 20         /// </summary>
 21         private string appAllPath
 22         {
 23             get
 24             {
 25                 return Process.GetCurrentProcess().MainModule.FileName;
 26             }
 27         }
 28         /// <summary>
 29         /// 桌面目录
 30         /// </summary>
 31         private string desktopPath
 32         {
 33             get
 34             {
 35                 return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
 36             }
 37         }
 38 
 39         /// <summary>
 40         /// 设置开机自动启动
 41         /// </summary>
 42         /// <param name="onOff"></param>
 43         public void SetMeAutoStart(bool onOff = true)
 44         {
 45             if (onOff) //开机启动
 46             {
 47                 //获取启动路径应用程序快捷键路径集合
 48                 List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
 49                 //存在两个以上快捷键方式则保留一个快捷方式
 50                 if(shortcutPaths.Count >= 2)
 51                 {
 52                     for(int i=0; i<shortcutPaths.Count; i++)
 53                     {
 54                         DeleteFile(shortcutPaths[i]);
 55                     }
 56                 }else if(shortcutPaths.Count < 1)
 57                 {
 58                     CreateShortcut(systemStartPath, QuickName, appAllPath, "订单自动导入");
 59                 }
 60             }
 61             else //开机不启动
 62             {
 63                 //获取启动路径应用程序快捷方式路径集合
 64                 List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
 65                 //存在快捷方式则遍历删除
 66                 if(shortcutPaths.Count > 0)
 67                 {
 68                     for(int i=0; i<shortcutPaths.Count; i++)
 69                     {
 70                         DeleteFile(shortcutPaths[i]);
 71                     }
 72                 }
 73             }
 74         }
 75         private bool CreateShortcut(string dir, string shortcutName, string targetPath, string description = null, string iconLocation = null)
 76         {
 77             try
 78             {
 79                 //目标目录不存在则创建
 80                 if (!Directory.Exists(dir))
 81                 {
 82                     Directory.CreateDirectory(dir);
 83                 }
 84                 //添加应用Com
 85                 string shortcutPath = Path.Combine(dir, string.Format("{0}.lnk", shortcutName)); //合成路径
 86                 WshShell shell = new IWshRuntimeLibrary.WshShell();
 87                 IWshShortcut shortcut = shell.CreateShortcut(shortcutPath);
 88                 shortcut.TargetPath = targetPath;
 89                 shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
 90                 shortcut.WindowStyle = 1;
 91                 shortcut.Description = description;
 92                 shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;
 93                 shortcut.Save();
 94                 return true;
 95             }
 96             catch (Exception e)
 97             {
 98                 Console.WriteLine(e.Message);
 99                 throw;
100             }
101         }
102         /// <summary>
103         /// 获取指定目录下指定程序的快捷方式路径集合
104         /// </summary>
105         /// <param name="dir"></param>
106         /// <param name="targetPath"></param>
107         /// <returns></returns>
108         private List<string> GetQuickFromFolder(string dir, string targetPath)
109         {
110             List<string> tempStrs = new List<string>();
111             tempStrs.Clear();
112             string tempStr = null;
113             string[] files = Directory.GetFiles(dir, "*.lnk");
114             if(files == null || files.Length < 1)
115             {
116                 return tempStrs;
117             }
118             for(int i=0; i<files.Length; i++)
119             {
120                 tempStr = GetAppPathFromQuick(files[i]);
121                 if(tempStr == targetPath)
122                 {
123                     tempStrs.Add(files[i]);
124                 }
125             }
126             return tempStrs;
127         }
128         /// <summary>
129         /// 获取快捷方式的目标文件路径-用于判断是否已经开启了自动启动
130         /// </summary>
131         /// <param name="shortcutPath"></param>
132         /// <returns></returns>
133         private string GetAppPathFromQuick(string shortcutPath)
134         {
135             //快捷方式文件的路径 = @"d:\Test.lnk";
136             if (System.IO.File.Exists(shortcutPath))
137             {
138                 WshShell shell = new WshShell();
139                 IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortcutPath);
140                 //快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
141                 //快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
142                 return shortct.TargetPath;
143             }
144             else
145             {
146                 return "";
147             }
148         }
149         /// <summary>
150         /// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
151         /// </summary>
152         /// <param name="path">路径</param>
153         private void DeleteFile(string path)
154         {
155             FileAttributes attr = System.IO.File.GetAttributes(path);
156             if (attr == FileAttributes.Directory)
157             {
158                 Directory.Delete(path, true);
159             }
160             else
161             {
162                 System.IO.File.Delete(path);
163             }
164         }
165         /// <summary>
166         /// 在桌面上创建快捷方式-如果需要可以调用
167         /// </summary>
168         /// <param name="desktopPath">桌面地址</param>
169         /// <param name="appPath">应用路径</param>
170         public void CreateDesktopQuick(string desktopPath = "", string quickName = "", string appPath = "")
171         {
172             List<string> shortcutPaths = GetQuickFromFolder(desktopPath, appPath);
173             //如果没有则创建
174             if (shortcutPaths.Count < 1)
175             {
176                 CreateShortcut(desktopPath, quickName, appPath, "客服销售订单自动导入");
177             }
178         }
179     }

  调用

 1         private void menuAutoStartOn_Click(object sender, EventArgs e)
 2         {
 3             KFSystemAutoStart auto = new KFSystemAutoStart();
 4             auto.SetMeAutoStart(true);
 5         }
 6 
 7         private void menuAutoStartOff_Click(object sender, EventArgs e)
 8         {
 9             KFSystemAutoStart auto = new KFSystemAutoStart();
10             auto.SetMeAutoStart(false);
11         }

 

2. 托盘图标

 1         //双击托盘图标
 2         private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
 3         {
 4             if (this.WindowState == FormWindowState.Minimized)
 5             {
 6                 WindowState = FormWindowState.Normal;
 7                 this.Activate();
 8                 this.ShowInTaskbar = true;
 9                 notifyIcon.Visible = true;
10             }
11         }
12 
13      //点击最小化,显示托盘图标,隐藏任务栏图标
14         private void KFSystemForm_SizeChanged(object sender, EventArgs e)
15         {
16             if (this.WindowState == FormWindowState.Minimized)
17             {
18                 this.ShowInTaskbar = false;
19                 notifyIcon.Visible = true;
20             }
21         }

 

(持续更新中) 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!