Sammy Ageil

Lead by example

Create windows 8 shutdown logoff and restart using C#

September 18
by Sammy Ageil 18. September 2012 23:12

If you installed Windows 8 you probably noticed the effort it takes to shutdown, restart or logoff your computer.
In this tutorial, we will relieve ourselves from this effort by creating the power shortcuts, place them on Windows 8 Tiles and assign them hotkeys.
Let's start..

  1. The first thing to do is to create a visual studio project, I choose a console application for simplicity reasons.
  2. Right click on References and add the following two references from the COM tab "Interop.IWshRuntimeLibrary & Interop.Shell32"
  3. Click on each of the added references and in the property window set Embeded Interop Types to false as shown in the screen capture

  4. Open the program.cs, delete all of the program class content "only leave the class declaration"
  5. Type in the following snippet
    const string LOGOFF = @"%systemroot%\System32\shell32.dll,44";
            const string REBOOT = @"%systemroot%\System32\shell32.dll,176";
            const string SHUTDOWN = @"%systemroot%\System32\shell32.dll,27";
    
            static void Main(string[] args)
            {
                Console.WriteLine("Enter one of the options to add the shortcut or pres esc or any other key to exit");
                Console.WriteLine("Z to add all shortcuts");
                Console.WriteLine("S to add shutdown only");
                Console.WriteLine("R to add a restart only");
                Console.WriteLine("L to logg off only");
              
                var response = Console.ReadKey(true);
                switch (response.Key)
                {
                    case  ConsoleKey.R:
                        CreateShortCut("r",REBOOT);
                        Exit();
                        break;
                    case ConsoleKey.S:
                        CreateShortCut("s", SHUTDOWN);
                        Exit();
                        break;
                    case ConsoleKey.L:
                        CreateShortCut("l", LOGOFF);
                     
                        break;
                    case ConsoleKey.Z:
                        CreateShortCut("s", SHUTDOWN);
                        CreateShortCut("r", REBOOT);
                        CreateShortCut("l", LOGOFF);
                        Exit();
                        break;
                    case ConsoleKey.Escape:
                        Exit();
                        break;
                    default:
                        Console.WriteLine("Invalid entry, restart the application and try again");
                        Console.ReadLine();
                        break;
                }
            
            }
            static void Exit()
            {
                Environment.Exit(0);
            }
            static void CreateShortCut(string option, string iconPath)
            {
                WshShell shellClass = new WshShell();
                var linkPath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
                var shortCutDescription = Description(option.ToLowerInvariant());
                var shortcut = (IWshShortcut)shellClass.CreateShortcut(String.Format("{0}\\{1}.lnk", linkPath, shortCutDescription));
                shortcut.IconLocation = iconPath;
               
                shortcut.WindowStyle = 1;
                shortcut.TargetPath = @"%systemroot%\System32\shutdown.exe";
                shortcut.Arguments = option == "l" ? "-l" : string.Format("-{0} -t 0", option.ToLower());
                shortcut.Hotkey = CreateHotKey(option);
                shortcut.Description = shortCutDescription;
                shortcut.Save();
            }
            static string CreateHotKey(string shortcut)
            {
                switch (shortcut)
                {
                    case "r":
                        return "Ctrl+Alt+R";
                    case "s":
                        return "Ctrl+Alt+S";
                    case "l":
                        return "Ctrl+Alt+L";
                    default:
                        return string.Empty;
                }
            }
            static string Description(string shortcut)
            {
                switch (shortcut)
                {
                    case "r":
                        return "Restart";
                    case "s":
                        return "Shutdown";
                    case "l":
                        return "LogOff";
                    default:
                        return string.Empty;
                }
            }
  6. Press F5 to start the application
  7. If you leave the code as is and press Z to add all power shortcuts, your shutdown hotkey will be Ctrl Alt S, reboot is Ctrl Alt R and logoff will be Ctrl Alt L
  8. Leave a comment if you need any assistance or you can download the code to play with

ShutDownConsole.zip (97.74 kb)

Tags: , ,

Windows 8

Comments (1) -

9/19/2012 9:01:42 PM #

Jason

Excellent write up.
Thank you,

Jason

Jason United States

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading