Advertisement
Koragg

PC AutoOff [C#]

Oct 16th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.33 KB | None | 0 0
  1. //https://www.codeproject.com/Tips/480049/Shut-Down-Restart-Log-off-Lock-Hibernate-or-Sleep -- Operations for PC
  2. //https://stackoverflow.com/questions/102567/how-to-shut-down-the-computer-from-c-sharp -- No console window flash
  3. //https://social.msdn.microsoft.com/Forums/vstudio/en-US/660a1f75-b287-4565-bfdd-75105e0a5527/c-wait-for-x-seconds?forum=netfxbcl -- Timer to wait 'x' milliseconds (app freezes)
  4.  
  5. using System;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace PCAutoOff
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int timeForActions = 0;
  16.             int timeValue = 0;
  17.             string timeString = "";
  18.             Console.WriteLine("This program will let you change power states on your computer after a set interval.");
  19.             Console.WriteLine("Available measurements: ");
  20.             Console.WriteLine("1 - Seconds");
  21.             Console.WriteLine("2 - Minutes");
  22.             Console.WriteLine("3 - Hours");
  23.             Console.Write("Please choose time measurement: ");
  24.             bool validTimeTypeChoice = false;
  25.             bool validTimeChoice = false;
  26.             int timeChoice = 0;
  27.             while (validTimeTypeChoice == false)
  28.             {
  29.                 try
  30.                 {
  31.                     timeChoice = Convert.ToInt32(Console.ReadLine());
  32.                     if ((timeChoice > 0) && (timeChoice < 4))
  33.                     {
  34.                         validTimeTypeChoice = true;
  35.                     }
  36.                     else
  37.                     {
  38.                         Console.Write("Please choose a measurement from the list: ");
  39.                     }
  40.                 }
  41.                 catch (System.Exception ex)
  42.                 {
  43.                     Console.Write(ex.Message + " Please choose a measurement: ");
  44.                 }
  45.             }
  46.             switch (timeChoice)
  47.             {
  48.                 case 1:
  49.                     Console.WriteLine("Time measurement chosen: seconds.");
  50.                     Console.Write("Please enter number of seconds after which action will activate: ");
  51.                     while (validTimeChoice == false)
  52.                     {
  53.                         try
  54.                         {
  55.                             int secs = Convert.ToInt32(Console.ReadLine());
  56.                             while (secs < 0)
  57.                             {
  58.                                 Console.Write("Please choose a positive value: ");
  59.                                 secs = Convert.ToInt32(Console.ReadLine());
  60.                             }
  61.                             timeForActions = secs * 1000;
  62.                             timeValue = secs;
  63.                             if (secs == 1)
  64.                             {
  65.                                 timeString = " second.";
  66.                             }
  67.                             else
  68.                             {
  69.                                 timeString = " seconds.";
  70.                             }
  71.                             validTimeChoice = true;
  72.                         }
  73.                         catch (System.Exception ex)
  74.                         {
  75.                             Console.Write(ex.Message + " Please enter number of seconds: ");
  76.                         }
  77.                     }
  78.                     break;
  79.                 case 2:
  80.                     Console.WriteLine("Time measurement chosen: minutes.");
  81.                     Console.Write("Please enter number of minutes after which action will activate: ");
  82.                     while (validTimeChoice == false)
  83.                     {
  84.                         try
  85.                         {
  86.                             int mins = Convert.ToInt32(Console.ReadLine());
  87.                             while (mins < 0)
  88.                             {
  89.                                 Console.Write("Please choose a positive value: ");
  90.                                 mins = Convert.ToInt32(Console.ReadLine());
  91.                             }
  92.                             timeForActions = mins * 60 * 1000;
  93.                             timeValue = mins;
  94.                             if (mins == 1)
  95.                             {
  96.                                 timeString = " minute.";
  97.                             }
  98.                             else
  99.                             {
  100.                                 timeString = " minutes.";
  101.                             }
  102.                             validTimeChoice = true;
  103.                         }
  104.                         catch (System.Exception ex)
  105.                         {
  106.                             Console.Write(ex.Message + " Please enter number of minutes: ");
  107.                         }
  108.                     }
  109.                     break;
  110.                 case 3:
  111.                     Console.WriteLine("Time measurement chosen: hours.");
  112.                     Console.Write("Please enter number of hours after which action will activate: ");
  113.                     while (validTimeChoice == false)
  114.                     {
  115.                         try
  116.                         {
  117.                             int hrs = Convert.ToInt32(Console.ReadLine());
  118.                             while (hrs < 0)
  119.                             {
  120.                                 Console.Write("Please choose a positive value: ");
  121.                                 hrs = Convert.ToInt32(Console.ReadLine());
  122.                             }
  123.                             timeForActions = hrs * 3600 * 1000;
  124.                             timeValue = hrs;
  125.                             if (hrs == 1)
  126.                             {
  127.                                 timeString = " hour.";
  128.                             }
  129.                             else
  130.                             {
  131.                                 timeString = " hours.";
  132.                             }
  133.                             validTimeChoice = true;
  134.                         }
  135.                         catch (System.Exception ex)
  136.                         {
  137.                             Console.Write(ex.Message + " Please enter number of hours: ");
  138.                         }
  139.                     }
  140.                     break;
  141.                 default:
  142.                     Console.WriteLine("You haven't chosen a time measurement!");
  143.                     break;
  144.             }
  145.             Console.WriteLine("Available actions: ");
  146.             Console.WriteLine("1 - Shutdown");
  147.             Console.WriteLine("2 - Restart");
  148.             Console.WriteLine("3 - Hibernate");
  149.             Console.WriteLine("4 - Sleep");
  150.             Console.WriteLine("5 - Log Off");
  151.             Console.WriteLine("6 - Lock");
  152.             Console.Write("Please choose an action: ");
  153.             bool validActionChoice = false;
  154.             int actionChoice = 0;
  155.             while (validActionChoice == false)
  156.             {
  157.                 try
  158.                 {
  159.                     actionChoice = Convert.ToInt32(Console.ReadLine());
  160.                     if ((actionChoice > 0) && (actionChoice < 7)){
  161.                         validActionChoice = true;
  162.                     }
  163.                     else
  164.                     {
  165.                         Console.Write("Please choose an action from the list: ");
  166.                     }
  167.                 }catch (System.Exception ex)
  168.                 {
  169.                     Console.Write(ex.Message + " Please choose an action: ");
  170.                 }
  171.             }
  172.             switch (actionChoice)
  173.             {
  174.                 case 1:
  175.                     Console.WriteLine("Shutting down computer after " + timeValue + timeString);
  176.                     System.Threading.Thread.Sleep(timeForActions);
  177.                     Console.WriteLine("Shutting down computer.");
  178.                     var psi1 = new ProcessStartInfo("shutdown", "/s /t 0");
  179.                     psi1.CreateNoWindow = true;
  180.                     psi1.UseShellExecute = false;
  181.                     Process.Start(psi1);
  182.                     break;
  183.                 case 2:
  184.                     Console.WriteLine("Restarting computer after " + timeValue + timeString);
  185.                     System.Threading.Thread.Sleep(timeForActions);
  186.                     Console.WriteLine("Restarting computer.");
  187.                     var psi2 = new ProcessStartInfo("shutdown", "/r /t 0");
  188.                     psi2.CreateNoWindow = true;
  189.                     psi2.UseShellExecute = false;
  190.                     Process.Start(psi2);
  191.                     break;
  192.                 case 3:
  193.                     Console.WriteLine("Hibernating computer after " + timeValue + timeString);
  194.                     System.Threading.Thread.Sleep(timeForActions);
  195.                     Console.WriteLine("Hibernating computer.");
  196.                     SetSuspendState(true, true, true);
  197.                     break;
  198.                 case 4:
  199.                     Console.WriteLine("Putting computer to sleep after " + timeValue + timeString);
  200.                     System.Threading.Thread.Sleep(timeForActions);
  201.                     Console.WriteLine("Putting computer to sleep.");
  202.                     SetSuspendState(false, true, true);
  203.                     break;
  204.                 case 5:
  205.                     Console.WriteLine("Logging off computer after " + timeValue + timeString);
  206.                     System.Threading.Thread.Sleep(timeForActions);
  207.                     Console.WriteLine("Logging off computer.");
  208.                     ExitWindowsEx(0, 0);
  209.                     break;
  210.                 case 6:
  211.                     Console.WriteLine("Locking computer after " + timeValue + timeString);
  212.                     System.Threading.Thread.Sleep(timeForActions);
  213.                     Console.WriteLine("Locking computer.");
  214.                     LockWorkStation();
  215.                     break;
  216.                 default:
  217.                     Console.WriteLine("You haven't chosen an action!");
  218.                     break;
  219.             }
  220.             Environment.Exit(0);
  221.         }
  222.  
  223.         //Hibernate & Sleep
  224.         [DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  225.         public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);
  226.  
  227.         //Lock
  228.         [DllImport("user32")]
  229.         public static extern void LockWorkStation();
  230.  
  231.         //Log off
  232.         [DllImport("user32")]
  233.         public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement