public async Task RebootAsync(BootEntry entry) { var output = await ExecuteBcdEditAsync("/bootsequence", entry.Identifier); if (output.Trim() != "The operation completed successfully.") throw new InvalidOperationException("Unable to set boot entry: " + output.Trim()); output = await ExecuteAsync("shutdown.exe", "-r", "-t", "0"); Application.Current.Shutdown(); }
public async void RebootTo(BootEntry entry) { try { await _bcd.RebootAsync(entry); } catch (Exception exception) { MessageBox.Show(exception.ToString(), "Error"); throw; } }
private bool ProcessCommandLine() { string[] args = Environment.GetCommandLineArgs(); if (args.Length > 1) { ushort entryId = 0; if (ushort.TryParse(args[1], out entryId)) { BootEntry entry = EFIEnvironment.GetEntries().FirstOrDefault(e => e.Id == entryId); if (entry != null) { DoReboot(entry, !args.Contains("quiet")); return(true); } } } return(false); }
public static void DoReboot(BootEntry entry, bool confirmation = true) { if (confirmation) { string message = string.Format("Are you sure want to reboot to {0} right now?", entry.Name); if (MessageBoxResult.Yes != MessageBox.Show(message, "Reboot confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)) { return; } } EFIEnvironment.SetBootNext(entry); if (!NativeMethods.ExitWindowsEx(ExitWindows.Reboot, ShutdownReason.MajorApplication | ShutdownReason.MinorInstallation | ShutdownReason.FlagPlanned)) { MessageBox.Show("Unable to reboot your computer", "Reboot failure", MessageBoxButton.OK, MessageBoxImage.Error); } }
public async Task<List<BootEntry>> GetBootEntriesAsync() { var output = await ExecuteBcdEditAsync(); var lines = output.Split('\n').Select(l => l.Trim()).ToList(); var separatorIndexes = lines.Select((l, i) => l == String.Empty ? i : -1).Where(i => i > 0).OrderBy(i => i); var entries = new List<BootEntry>(); var firstLine = 1; foreach (var separatorIndex in separatorIndexes) { var entryLines = lines.Skip(firstLine).Take(separatorIndex - firstLine).ToArray(); if (entryLines.Length < 2) throw new InvalidOperationException($"Error parsing entry from {firstLine} to {separatorIndex}: " + String.Join("\r\n", entryLines)); if (entryLines[1].IndexOf('-') == -1) throw new InvalidOperationException($"Missing separator line from entry at {firstLine} to {separatorIndex}: " + String.Join("\r\n", entryLines)); var entry = new BootEntry(entryLines); if (entry.Description != "Windows Boot Manager") entries.Add(entry); firstLine = separatorIndex + 1; } return entries; }
public static bool SetBootNext(BootEntry entry) { return(SetBootNext(entry.Id)); }