| 1 | using Microsoft.Win32; |
| 2 | using Microsoft.Win32.TaskScheduler; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.IO; |
| 6 | using System.Linq; |
| 7 | using System.Management; |
| 8 | using System.Text; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace Core.Librarys |
| 12 | { |
| 13 | public class SystemCommon |
| 14 | { |
| 15 | ///<summary> |
| 16 | /// |
| 17 | ///</summary> |
| 18 | ///<param name="startup"></param> |
| 19 | ///<returns></returns> |
| 20 | public static bool SetStartup(bool startup = true) |
| 21 | { |
| 22 | string TaskName = "Tai task"; |
| 23 | var logonUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name; |
| 24 | string tai = Path.Combine( |
| 25 | AppDomain.CurrentDomain.BaseDirectory, |
| 26 | "Tai.exe"); |
| 27 | string taskDescription = "Tai开机自启服务"; |
| 28 | |
| 29 | using (var taskService = new TaskService()) |
| 30 | { |
| 31 | var tasks = taskService.RootFolder.GetTasks(new System.Text.RegularExpressions.Regex(TaskName)); |
| 32 | foreach (var t in tasks) |
| 33 | { |
| 34 | taskService.RootFolder.DeleteTask(t.Name); |
| 35 | } |
| 36 | |
| 37 | if (startup) |
| 38 | { |
| 39 | var task = taskService.NewTask(); |
| 40 | task.RegistrationInfo.Description = taskDescription; |
| 41 | task.Triggers.Add(new LogonTrigger { UserId = logonUser }); |
| 42 | task.Principal.RunLevel = TaskRunLevel.Highest; |
| 43 | task.Actions.Add(new ExecAction(tai, "--selfStart", AppDomain.CurrentDomain.BaseDirectory)); |
| 44 | task.Settings.StopIfGoingOnBatteries = false; |
| 45 | task.Settings.DisallowStartIfOnBatteries = false; |
| 46 | taskService.RootFolder.RegisterTaskDefinition(TaskName, task); |
| 47 | } |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | public static string GetWindowsVersionName() |
| 53 | { |
| 54 | string name = string.Empty; |
| 55 | try |
| 56 | { |
| 57 | ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); |
| 58 | foreach (ManagementObject obj in managementObjectSearcher.Get()) |
| 59 | { |
| 60 | name = obj["Name"].ToString(); |
| 61 | } |
| 62 | |
| 63 | if (!string.IsNullOrEmpty(name) && name.IndexOf("|") != -1) |
| 64 | { |
| 65 | name = name.Split('|')[0]; |
| 66 | } |
| 67 | } |
| 68 | catch |
| 69 | { |
| 70 | return "[无法获取系统版本]"; |
| 71 | } |
| 72 | |
| 73 | return name; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | } |
| 78 | } |