Advertisement
LaPanthere

Task Class

Nov 6th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4.  
  5. namespace ...
  6. {
  7.     class Task
  8.     {
  9.         public string TaskName;
  10.         public string FileName;
  11.  
  12.         public enum Trigger
  13.         {
  14.             atLogon,
  15.             atStartup,
  16.             atCreation
  17.         }
  18.  
  19.         public Task()
  20.         {
  21.             if (!isAdmin())
  22.             {
  23.                 throw new UnauthorizedAccessException("The user is currently not an admin so tasks cannot be managed");
  24.             }
  25.         }
  26.  
  27.         public bool isAdmin()
  28.         {
  29.             bool admin = false;
  30.             string testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "_.txt");
  31.             try
  32.             {
  33.                 File.Create(testFile);
  34.  
  35.                 if (File.Exists(testFile))
  36.                 {
  37.                     admin = true;
  38.                     File.Delete(testFile);
  39.                     return admin;
  40.                 }
  41.             }
  42.             catch
  43.             {
  44.             }
  45.             return admin;
  46.         }
  47.  
  48.         public bool checkExists()
  49.         {
  50.             ProcessStartInfo start = new ProcessStartInfo();
  51.             start.FileName = "schtasks.exe";
  52.             start.UseShellExecute = false;
  53.             start.CreateNoWindow = true;
  54.             start.WindowStyle = ProcessWindowStyle.Hidden;
  55.             start.Arguments = "/query /TN \"" + TaskName + "\"";
  56.             start.RedirectStandardOutput = true;
  57.  
  58.             using (Process process = Process.Start(start))
  59.             {
  60.                 using (StreamReader reader = process.StandardOutput)
  61.                 {
  62.                     string stdout = reader.ReadToEnd();
  63.                     if (stdout.Contains(TaskName))
  64.                     {
  65.                         return true;
  66.                     }
  67.                     else
  68.                     {
  69.                         return false;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.  
  75.         public bool createTask(Trigger t)
  76.         {
  77.             string tt = "";
  78.             switch (t)
  79.             {
  80.                 case Trigger.atLogon:
  81.                     tt = "ONLOGON";
  82.                     break;
  83.                 case Trigger.atCreation:
  84.                     tt = "ONCE";
  85.                     break;
  86.                 case Trigger.atStartup:
  87.                     tt = "ONSTARTUP";
  88.                     break;
  89.             }
  90.             return runCMD("schtasks /create /ru \"SYSTEM\" /sc " + tt + " /tn \"" + TaskName + "\" /tr \"" + FileName + "\" /rl HIGHEST /f");
  91.         }
  92.  
  93.         public bool disableTask()
  94.         {
  95.             return runCMD("schtasks /change /tn \"" + TaskName + "\" /disable");
  96.         }
  97.  
  98.         public bool enableTask()
  99.         {
  100.             return runCMD("schtasks /change /tn \"" + TaskName + "\" /enable");
  101.         }
  102.  
  103.         public bool deleteTask()
  104.         {
  105.             return runCMD("schtasks /delete \"" + TaskName + "\"");
  106.         }
  107.  
  108.         public bool runTask()
  109.         {
  110.             return runCMD("schtasks /run \"" + TaskName + "\"");
  111.         }
  112.  
  113.         private bool runCMD(string command, bool asAdmin = true)
  114.         {
  115.             try
  116.             {
  117.                 Process.Start(
  118.                      new ProcessStartInfo()
  119.                      {
  120.                          FileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\cmd.exe",
  121.                          Verb = asAdmin ? "runas" : "",
  122.                          Arguments = "/c " + command,
  123.                          WindowStyle = ProcessWindowStyle.Hidden
  124.                      }
  125.                  );
  126.             }
  127.             catch
  128.             {
  129.                 return false;
  130.             }
  131.             return true;
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement