跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

Shortcut.cs

76 行 · 2.4 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Reflection;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace Core.Librarys
10{
11 public class Shortcut
12 {
13 /// <summary>
14 /// 为本程序创建一个快捷方式。
15 /// </summary>
16 /// <param name="lnkFilePath">快捷方式的完全限定路径。</param>
17 /// <param name="args">快捷方式启动程序时需要使用的参数。</param>
18 private static bool CreateShortcut(string lnkFilePath, string args)
19 {
20 try
21 {
22 var shellType = Type.GetTypeFromProgID("WScript.Shell");
23 dynamic shell = Activator.CreateInstance(shellType);
24 var shortcut = shell.CreateShortcut(lnkFilePath);
25 shortcut.TargetPath = Assembly.GetEntryAssembly().Location;
26 shortcut.Arguments = args;
27 shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
28 shortcut.Save();
29 return true;
30 }
31 catch (Exception e)
32 {
33 Logger.Error(e.ToString());
34 return false;
35 }
36 }
37 /// <summary>
38 /// 设置开机启动
39 /// </summary>
40 /// <param name="startup"></param>
41 /// <returns></returns>
42 public static bool SetStartup(bool startup = true)
43 {
44 string path = Path.Combine(
45 Environment.GetFolderPath(Environment.SpecialFolder.Startup),
46 "Tai.lnk");
47 if (startup)
48 {
49 if (File.Exists(path))
50 {
51 return true;
52 }
53 return CreateShortcut(path, "");
54 }
55 else
56 {
57 if (File.Exists(path))
58 {
59 File.Delete(path);
60 }
61 return true;
62 }
63 }
64 /// <summary>
65 /// 创建桌面快捷方式
66 /// </summary>
67 /// <param name="create"></param>
68 /// <returns></returns>
69 public static bool CreateDesktopShortcut()
70 {
71 return CreateShortcut(Path.Combine(
72 Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
73 "Tai.lnk"), "--selfStart");
74 }
75 }
76}