使用C#获取windows开机启动所有项的路径与类型

使用C#获取windows开机启动所有项的路径与类型这个类共获取的项有三类,分别是注册表有几个特殊的路径是开机启动的路径、还有几个特殊文件夹以及WINDOWS任务启动。直接上干货,注意,需要引用

大家好,欢迎来到IT知识分享网。

这个类共获取的项有三类,分别是注册表有几个特殊的路径是开机启动的路径、还有几个特殊文件夹以及WINDOWS任务启动。

使用C#获取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; } } } 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/80487.html

(0)
上一篇 2024-08-27 21:45
下一篇 2024-08-30 18:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信