garrettboast

Computer Locker

Jul 21st, 2014
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Microsoft.Win32;
  10. using System.Runtime.InteropServices;
  11.  
  12. namespace ComputerLocker
  13. {
  14.    
  15.  
  16.     public partial class frmMain : Form
  17.     {
  18.         [DllImport("user32")]
  19.         public static extern void LockWorkStation();
  20.  
  21.         public bool SessionLocked = false;
  22.  
  23.         public frmMain()
  24.         {
  25.             Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
  26.             InitializeComponent();
  27.         }
  28.  
  29.         private bool checkLockCondition()
  30.         {
  31.             bool result = false;
  32.             if (this.SessionLocked)
  33.             {
  34.                 return false;
  35.             }
  36.  
  37.             if (DateTime.Now.Hour > 1 && DateTime.Now.Hour < 6 && DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
  38.             {
  39.                 result = true;
  40.             }
  41.  
  42.             return result;
  43.         }
  44.        
  45.         private void lockPc()
  46.         {
  47.             LockWorkStation();
  48.         }
  49.  
  50.         private void tmrCheckLock_Tick(object sender, EventArgs e)
  51.         {
  52.             if (checkLockCondition())
  53.             {
  54.                 lockPc();
  55.             }
  56.         }
  57.  
  58.         private void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
  59.         {
  60.             if(e.Reason == SessionSwitchReason.SessionUnlock)
  61.             {
  62.                 this.SessionLocked = false;
  63.                 if(checkLockCondition())
  64.                 {
  65.                     lockPc();
  66.                 }
  67.             }
  68.             if(e.Reason == SessionSwitchReason.SessionLock)
  69.             {
  70.                 this.SessionLocked = true;
  71.             }
  72.         }
  73.  
  74.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
  75.         {
  76.             e.Cancel = true;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment