/// <summary>
        ///     Set if this startup entry should run for all users or only for the current user.
        /// </summary>
        public static void SetAllUsers(StartupEntry startupEntry, bool allUsers)
        {
            // Find the suitable replacement
            var target = StartupEntryFactory.RunLocations.First(x => (x.IsRegKey == startupEntry.IsRegKey) &&
                                                                (x.IsRunOnce == startupEntry.IsRunOnce) &&
                                                                (x.AllUsers == allUsers) && !x.IsWow);

            // Don't want to deal with the disable wizardry
            var wasDisabled = startupEntry.Disabled;

            if (wasDisabled)
            {
                Enable(startupEntry);
            }

            // Remove old entry or move the link to the new directory.
            if (startupEntry.IsRegKey)
            {
                try
                {
                    // Can't do this with links as they would get deleted
                    startupEntry.Delete();
                }
                catch
                {
                    // Key doesn't exist
                }
            }
            else
            {
                if (File.Exists(startupEntry.FullLongName))
                {
                    var newPath = Path.Combine(target.Path, startupEntry.EntryLongName);
                    File.Delete(newPath);
                    File.Move(startupEntry.FullLongName, newPath);
                }
            }

            // Plug in new data
            startupEntry.SetParentLongName(target.Path);
            startupEntry.AllUsersStore = allUsers;

            // Update registry stuff
            if (startupEntry.IsRegKey)
            {
                CreateRegValue(startupEntry);
            }

            // Restore disable status
            if (wasDisabled)
            {
                Disable(startupEntry);
            }
        }
Esempio n. 2
0
        public void Disable(StartupEntry startupEntry)
        {
            if (startupEntry.DisabledStore)
            {
                return;
            }

            var newPath = CreateDisabledEntryPath(startupEntry);

            // Remove the startup entry / move the file to the backup folder
            if (startupEntry.IsRegKey)
            {
                try
                {
                    startupEntry.Delete();
                }
                catch
                {
                    // Key doesn't exist
                }
            }
            else
            {
                if (File.Exists(startupEntry.FullLongName))
                {
                    File.Delete(newPath);
                    if (!Directory.Exists(DriveDisableBackupPath))
                    {
                        Directory.CreateDirectory(DriveDisableBackupPath);
                    }
                    File.Move(startupEntry.FullLongName, newPath);
                    startupEntry.BackupPath = newPath;
                }
                else
                {
                    throw new InvalidOperationException(Localisation.StartupManager_FailedEnable_FileNotFound
                                                        + "\n\n" + startupEntry.FullLongName);
                }
            }

            CreateDisabledEntry(startupEntry, newPath);

            startupEntry.DisabledStore = true;
        }
        public static void MoveToRegistry(StartupEntry startupEntry)
        {
            if (startupEntry.IsRegKey)
            {
                return;
            }

            // Don't want to deal with the disable wizardry
            var wasDisabled = startupEntry.Disabled;

            if (wasDisabled)
            {
                Enable(startupEntry);
            }

            // Delete old entry
            startupEntry.Delete();

            // Plug in new data
            startupEntry.IsRegKey = true;

            var newPoint =
                StartupEntryFactory.RunLocations.First(x => x.IsRegKey && (x.AllUsers == startupEntry.AllUsers) &&
                                                       !x.IsRunOnce && !x.IsWow);

            startupEntry.SetParentLongName(newPoint.Path);
            startupEntry.SetParentFancyName(newPoint.Name);

            // Recreate registry entry
            CreateRegValue(startupEntry);

            // Restore disable status
            if (wasDisabled)
            {
                Disable(startupEntry);
            }
        }