Пример #1
0
        /// <summary>
        /// Returns the list of <see cref="AutorunObject"/> objects that have been added through Start menu directory
        /// </summary>
        public List <AutorunObject> GetAutorunObjects()
        {
            List <AutorunObject> resultObjectsList = new List <AutorunObject>();

            DirectoryInfo directoryInfo = new DirectoryInfo(_startupDirectoryPath);

            if (!directoryInfo.Exists)
            {
                return(null);
            }

            // If need show all files remove searchPattern string
            var files = directoryInfo.GetFiles("*.lnk");

            foreach (var file in files)
            {
                WshShell shell = new WshShell();

                IWshShortcut link = (IWshShortcut)shell.CreateShortcut(file.FullName);

                string targetPath = link.TargetPath;
                string arguments  = link.Arguments;

                var autorunObject = AutorunObjectHelper.GetAutorunObject(targetPath, arguments, AutorunTypes.StartMenu);

                if (autorunObject != null)
                {
                    resultObjectsList.Add(autorunObject);
                }
            }

            return(resultObjectsList);
        }
        /// <summary>
        /// Returns the list of <see cref="AutorunObject"/> objects that have been added through TaskScheduler
        /// </summary>
        public List <AutorunObject> GetAutorunObjects()
        {
            List <AutorunObject> resultObjectsList = new List <AutorunObject>();

            TaskScheduler.TaskScheduler taskService = new TaskScheduler.TaskScheduler();
            taskService.Connect();

            ITaskFolder rootFolder = taskService.GetFolder(@"\");

            IRegisteredTaskCollection regTask = rootFolder.GetTasks(0);

            foreach (IRegisteredTask task in regTask)
            {
                ITaskDefinition    taskDefinition    = task.Definition;
                ITriggerCollection triggerCollection = taskDefinition.Triggers;

                foreach (ITrigger trigger in triggerCollection)
                {
                    if (trigger.Type == _TASK_TRIGGER_TYPE2.TASK_TRIGGER_BOOT ||
                        trigger.Type == _TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON)
                    {
                        IActionCollection actionCollection = taskDefinition.Actions;

                        foreach (IExecAction action in actionCollection)
                        {
                            if (action.Type != _TASK_ACTION_TYPE.TASK_ACTION_EXEC)
                            {
                                continue;
                            }

                            var autorunObject = AutorunObjectHelper.GetAutorunObject(action.Path, action.Arguments, AutorunTypes.Scheduler);

                            if (autorunObject != null)
                            {
                                resultObjectsList.Add(autorunObject);
                            }
                        }
                    }
                }
            }

            return(resultObjectsList);
        }
        /// <summary>
        /// Returns the list of <see cref="AutorunObject"/> objects that have been added through Registry
        /// </summary>
        public List <AutorunObject> GetAutorunObjects()
        {
            List <AutorunObject> resultObjectsList = new List <AutorunObject>();
            List <RegistryKey>   registryKeys      = new List <RegistryKey>();

            RegistryKey currentUserRegistryKey      = Registry.CurrentUser.OpenSubKey(RegistryNodePath, true);
            RegistryKey currentUserRegistryKey6432  = Registry.CurrentUser.OpenSubKey(RegistryNodePath6432, true);
            RegistryKey localMachineRegistryKey     = Registry.LocalMachine.OpenSubKey(RegistryNodePath, true);
            RegistryKey localMachineRegistryKey6432 = Registry.LocalMachine.OpenSubKey(RegistryNodePath6432, true);

            registryKeys.Add(currentUserRegistryKey);
            registryKeys.Add(currentUserRegistryKey6432);
            registryKeys.Add(localMachineRegistryKey);
            registryKeys.Add(localMachineRegistryKey6432);

            registryKeys.ForEach(LoadObjects);

            return(resultObjectsList);

            void LoadObjects(RegistryKey regKey)
            {
                if (regKey == null)
                {
                    return;
                }

                foreach (var keyName in regKey.GetValueNames())
                {
                    var    subkey        = regKey.GetValue(keyName);
                    string firstPattern  = "(\"(?<path>.*?)\"\\s)(?<arguments>.*)";
                    string secondPattern = "((?<path>.*)\\s(?<arguments>.*))";

                    string filePath  = string.Empty;
                    string arguments = string.Empty;
                    bool   isAutorunObject;

                    // Checking for Pattern Matching
                    if (File.Exists(subkey.ToString()))
                    {
                        filePath        = subkey.ToString();
                        isAutorunObject = true;
                    }
                    else if (CheckIsMatch(firstPattern, subkey.ToString(), out filePath, out arguments))
                    {
                        isAutorunObject = true;
                    }
                    else
                    {
                        isAutorunObject = CheckIsMatch(secondPattern, subkey.ToString(), out filePath, out arguments);
                    }

                    if (!isAutorunObject)
                    {
                        return;
                    }

                    var autorunObject = AutorunObjectHelper.GetAutorunObject(filePath, arguments, AutorunTypes.Registry);

                    if (autorunObject != null)
                    {
                        resultObjectsList.Add(autorunObject);
                    }
                }
            }
        }