欢迎大家来到IT世界,在知识的湖畔探索吧!
这个类共获取的项有三类,分别是注册表有几个特殊的路径是开机启动的路径、还有几个特殊文件夹以及WINDOWS任务启动。
直接上干货,注意,需要引用 Microsoft.Win32,别忘记了。
using Microsoft.Win32; using Microsoft.Win32.TaskScheduler; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EAP.Windows { /// <summary> /// 调用例子 /// <code> /// StartupManager manager = new StartupManager(); /// List<StartupItem> startupItems = manager.GetStartupItems(); /// foreach (var item in startupItems) /// { /// Console.WriteLine(#34;开机启动类型: {item.Location}, 路径: {item.Path}"); /// } /// </code> /// </summary> public class StartupManager { /// <summary> /// 获取所有开机启动项 /// </summary> /// <returns></returns> public List<StartupItem> GetStartupItems() { var startupItems = new List<StartupItem>(); // 读取所有开机启动注册表项 ReadRegistryStartupItems(Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"), StartupLocation.RegistryRun, startupItems); ReadRegistryStartupItems(Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\RunOnce"), StartupLocation.RegistryRunOnce, startupItems); ReadRegistryStartupItems(Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"), StartupLocation.RegistryRun, startupItems); ReadRegistryStartupItems(Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\RunOnce"), StartupLocation.RegistryRunOnce, startupItems); // 读取所有开机启动特殊文件夹项 ReadStartupFolderItems(Environment.GetFolderPath(Environment.SpecialFolder.Startup), StartupLocation.CurrentUserStartupFolder, startupItems); ReadStartupFolderItems(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup), StartupLocation.AllUsersStartupFolder, startupItems); // 读取所有开机WINDOWS任务项 ReadTaskSchedulerItems(startupItems); return startupItems; } /// <summary> /// 读取注册表的项 /// </summary> /// <param name="registryKey">注册表路径</param> /// <param name="location">注册表开机启动类型</param> /// <param name="startupItems">结果列表</param> private void ReadRegistryStartupItems(RegistryKey registryKey, StartupLocation location, List<StartupItem> startupItems) { if (registryKey == null) return; foreach (var valueName in registryKey.GetValueNames()) { startupItems.Add(new StartupItem { Path = registryKey.GetValue(valueName).ToString(), Location = location }); } } /// <summary> /// 获取特殊文件夹开机启动项 /// </summary> /// <param name="folderPath">文件夹路径</param> /// <param name="location">启动类型</param> /// <param name="startupItems">结果列表</param> private void ReadStartupFolderItems(string folderPath, StartupLocation location, List<StartupItem> startupItems) { if (!Directory.Exists(folderPath)) return; foreach (var filePath in Directory.GetFiles(folderPath)) { startupItems.Add(new StartupItem { Path = filePath, Location = location }); } } /// <summary> /// 获取WINDOWS任务开机启动项 /// </summary> /// <param name="startupItems">结果列表</param> private void ReadTaskSchedulerItems(List<StartupItem> startupItems) { using (TaskService taskService = new TaskService()) { foreach (var task in taskService.AllTasks) { if (task.Definition.Triggers.Count > 0) { foreach (var trigger in task.Definition.Triggers) { if (trigger is LogonTrigger || trigger is BootTrigger) { startupItems.Add(new StartupItem { Path = task.Path, Location = StartupLocation.TaskScheduler }); break; } } } } } } } /// <summary> /// 启动项类型 /// </summary> public enum StartupLocation { /// <summary> /// 注册表 /// </summary> RegistryRun, /// <summary> /// 注册表只启动一次 /// </summary> RegistryRunOnce, /// <summary> /// 当前用户启动项目录 /// </summary> CurrentUserStartupFolder, /// <summary> /// 所有启动项目录 /// </summary> AllUsersStartupFolder, /// <summary> /// 任务启动 /// </summary> TaskScheduler } /// <summary> /// 启动项 /// </summary> public class StartupItem { /// <summary> /// 启动路径 /// </summary> public string Path { get; set; } /// <summary> /// 启动类型 /// </summary> public StartupLocation Location { get; set; } } }
欢迎大家来到IT世界,在知识的湖畔探索吧!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/70169.html