Exemple #1
0
        public static IEnumerable <ConsoleColorOutput> Run(StartupProgram program)
        {
            var existingPrograms     = ListCommandHandler.Run().Programs.ToList();
            var programAlreadyStarts = existingPrograms.FirstOrDefault(x => x.Path.Contains(program.File.FullName, StringComparison.OrdinalIgnoreCase) && !x.Disabled);
            var nameInUseBy          = existingPrograms.FirstOrDefault(x => x.Name.Equals(program.Name, StringComparison.OrdinalIgnoreCase));

            if (nameInUseBy != null)
            {
                return(new [] {
                    new ConsoleColorOutput(WriteMode.Write, program.Name, ConsoleColor.Yellow),
                    new ConsoleColorOutput(WriteMode.Writeline, " is already in use, please try again with a different name", ConsoleColor.Red),
                });
            }

            if (programAlreadyStarts != null)
            {
                var messages = new [] {
                    new ConsoleColorOutput(WriteMode.Write, program.File.Name, ConsoleColor.Yellow),
                    new ConsoleColorOutput(WriteMode.Writeline, " already starts with windows", ConsoleColor.Red),
                    new ConsoleColorOutput(WriteMode.Write, "Want to add another instance of it to startup? y/n: "),
                };
                ConsoleOutputWriter.WriteToConsole(messages);

                var userWantsToContinue = ConsoleStepWizard.PromptUserForBool("y", "n", $"Want to add another? y/n: ");
                if (!userWantsToContinue)
                {
                    return(new List <ConsoleColorOutput>());
                }
            }

            if (program.AllUsers || program.Administrator)
            {
                if (!WindowsIdentityService.IsElevated())
                {
                    return(new [] {
                        new ConsoleColorOutput(WriteMode.Writeline, "This requires you run the command as administrator", ConsoleColor.Red)
                    });
                }
            }

            //Current user only programs requires a schedule task to run as Administrator
            if (program.Administrator && !program.AllUsers)
            {
                TaskSchedulerService.AddProgramToStartup(program);
            }
            else
            {
                RegistryService.AddProgramToStartup(program);
            }
            return(new [] {
                new ConsoleColorOutput(WriteMode.Write, "Added "),
                new ConsoleColorOutput(WriteMode.Write, program.Name, ConsoleColor.Yellow),
                new ConsoleColorOutput(WriteMode.Writeline, " to startup")
            });
        }
Exemple #2
0
        public static IEnumerable <ConsoleColorOutput> Run(string name, bool confirm)
        {
            var program = ListCommandHandler.Run().Programs.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

            if (program == null)
            {
                return(new [] {
                    new ConsoleColorOutput(WriteMode.Write, "Could not find a program with name ", ConsoleColor.Red),
                    new ConsoleColorOutput(WriteMode.Writeline, name, ConsoleColor.Yellow),
                });
            }

            if (program.RequireAdministrator && !WindowsIdentityService.IsElevated())
            {
                return(new [] {
                    new ConsoleColorOutput(WriteMode.Write, $"To modify settings for ", ConsoleColor.Red),
                    new ConsoleColorOutput(WriteMode.Write, program.Name, ConsoleColor.Yellow),
                    new ConsoleColorOutput(WriteMode.Writeline, " you need to run the command with administrator (sudo)", ConsoleColor.Red),
                });
            }

            if (!confirm)
            {
                var message  = $"Are you sure you want to remove '{program.Name}' y/n: ";
                var messages = new [] {
                    new ConsoleColorOutput(WriteMode.Write, message),
                };
                ConsoleOutputWriter.WriteToConsole(messages);
                var confirmation = ConsoleStepWizard.PromptUserForBool("y", "n", message);
                if (!confirmation)
                {
                    return(new List <ConsoleColorOutput>());
                }
            }

            switch (program.Type)
            {
            case Models.StartupList.StartupType.TaskScheduler:
                TaskSchedulerService.RemoveProgramFromStartup(program.Name);
                break;

            case Models.StartupList.StartupType.Shortcut:
                DirectoryService.RemoveProgramFromStartup(program);
                break;

            case Models.StartupList.StartupType.Regedit:
                RegistryService.RemoveProgramFromStartup(program);
                break;
            }
            return(new [] {
                new ConsoleColorOutput(WriteMode.Write, program.Name, ConsoleColor.Yellow),
                new ConsoleColorOutput(WriteMode.Writeline, " has been removed"),
            });
        }