Exemplo n.º 1
0
        private List <StartupEntry> GetAppsFromDirectory(StartupLocation location)
        {
            List <StartupEntry> startupApps     = new List <StartupEntry>();
            List <string>       disallowedItems = GetDisallowedItems(location);
            string locationExpanded             = Environment.ExpandEnvironmentVariables(location.Location);

            try
            {
                if (Shell.Exists(locationExpanded))
                {
                    SystemDirectory directory = new SystemDirectory(locationExpanded, Dispatcher.CurrentDispatcher, false);

                    foreach (SystemFile startupFile in directory.Files)
                    {
                        // only add items that are not disabled
                        if (!disallowedItems.Contains(startupFile.Name))
                        {
                            startupApps.Add(new StartupEntry
                            {
                                Location = location,
                                Path     = startupFile.FullName
                            });
                        }
                    }

                    directory.Dispose();
                }
            }
            catch
            {
                CairoLogger.Instance.Warning($"StartupRunner: Unable to load startup items from directory {location}");
            }

            return(startupApps);
        }
Exemplo n.º 2
0
        private List <StartupEntry> GetAppsFromEntry(StartupLocation location)
        {
            switch (location.Type)
            {
            case StartupEntryType.Directory:
                return(GetAppsFromDirectory(location));

            case StartupEntryType.RegistryKey:
                return(GetAppsFromRegistryKey(location));

            default:
                CairoLogger.Instance.Debug("StartupRunner: Unknown startup location type");
                break;
            }

            return(new List <StartupEntry>());
        }
Exemplo n.º 3
0
        private List <string> GetDisallowedItems(StartupLocation location, StartupEntryScope?overrideScope = null)
        {
            List <string> disallowedApps = new List <string>();

            if (!string.IsNullOrEmpty(location.ApprovedLocation))
            {
                StartupEntryScope scope = overrideScope ?? location.Scope;
                RegistryKey[]     roots = ScopeToRoots(scope);

                foreach (var root in roots)
                {
                    try
                    {
                        RegistryKey registryKey =
                            root.OpenSubKey(location.ApprovedLocation, false);

                        if (registryKey != null && registryKey.ValueCount > 0)
                        {
                            foreach (var valueName in registryKey.GetValueNames())
                            {
                                if (((byte[])registryKey.GetValue(valueName))[0] % 2 != 0) // if value is odd number, item is disabled
                                {
                                    disallowedApps.Add(valueName);
                                    CairoLogger.Instance.Debug($"StartupRunner: Skipping disabled entry: {valueName}");
                                }
                            }
                        }

                        // close key when finished
                        registryKey?.Close();
                    }
                    catch
                    {
                        CairoLogger.Instance.Warning($"StartupRunner: Unable to load allowed startup items list from registry key {location.ApprovedLocation}");
                    }
                }
            }

            return(disallowedApps);
        }
Exemplo n.º 4
0
        private List <StartupEntry> GetAppsFromDirectory(StartupLocation location)
        {
            List <StartupEntry> startupApps     = new List <StartupEntry>();
            List <string>       disallowedItems = GetDisallowedItems(location);
            string locationExpanded             = Environment.ExpandEnvironmentVariables(location.Location);

            try
            {
                if (ShellHelper.Exists(locationExpanded))
                {
                    foreach (string startupFile in Directory.EnumerateFiles(locationExpanded))
                    {
                        if (!ShellHelper.IsFileVisible(startupFile))
                        {
                            continue;
                        }

                        // only add items that are not disabled
                        if (!disallowedItems.Contains(Path.GetFileName(startupFile)))
                        {
                            startupApps.Add(new StartupEntry
                            {
                                Location = location,
                                Path     = startupFile
                            });
                        }
                    }
                }
            }
            catch
            {
                ShellLogger.Warning($"StartupRunner: Unable to load startup items from directory {location}");
            }

            return(startupApps);
        }
Exemplo n.º 5
0
        private List <StartupEntry> GetAppsFromRegistryKey(StartupLocation location)
        {
            RegistryKey[]       roots       = ScopeToRoots(location.Scope);
            List <StartupEntry> startupApps = new List <StartupEntry>();

            foreach (var root in roots)
            {
                bool isRunOnce = location.Location.Contains("RunOnce");

                try
                {
                    if (isRunOnce && root == Registry.LocalMachine)
                    {
                        continue; // skip HKLM RunOnce since we cannot delete these items, and would run them each startup
                    }
                    RegistryKey registryKey =
                        root.OpenSubKey(location.Location, root == Registry.CurrentUser); // open as writeable if HKCU

                    if (registryKey != null && registryKey.ValueCount > 0)
                    {
                        // get list of disallowed entries
                        List <string> disallowedItems = GetDisallowedItems(location, root == Registry.LocalMachine ? StartupEntryScope.Machine : StartupEntryScope.User);

                        // add items from registry key
                        foreach (var valueName in registryKey.GetValueNames())
                        {
                            // only add items that are not disabled
                            if (!disallowedItems.Contains(valueName))
                            {
                                startupApps.Add(new StartupEntry
                                {
                                    Location = location,
                                    Path     = ((string)registryKey.GetValue(valueName)).Replace("\"", "")
                                });

                                // if this is a runonce key, remove the value after we grab it
                                if (isRunOnce)
                                {
                                    try
                                    {
                                        registryKey.DeleteValue(valueName);
                                    }
                                    catch
                                    {
                                        CairoLogger.Instance.Warning($"StartupRunner: Unable to delete RunOnce startup item {valueName}");
                                    }
                                }
                            }
                        }
                    }

                    // close key when finished
                    registryKey?.Close();
                }
                catch
                {
                    CairoLogger.Instance.Warning($"StartupRunner: Unable to load startup items from registry key {location.Location}");
                }
            }

            return(startupApps);
        }