LabtechConsulting
4/27/2017 - 4:25 PM

LabTech Uninstaller

LabTech Uninstaller

using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace LabUninstall
{
  [StandardModule]
  internal sealed class modMain
  {
    public static List<string> RemoteAgentIdentifiers = new List<string>((IEnumerable<string>) new string[4]
    {
      "LabTech Agent Service",
      "LabTech® Agent Service",
      "LabTech Software Remote Agent",
      "LabTech® Software Remote Agent"
    });

    public static bool ISVista()
    {
      return Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6;
    }

    public static bool IsElevated()
    {
      TOKEN_ELEVATION structure;
      structure.TokenIsElevated = 0;
      int num = Marshal.SizeOf((object) structure);
      IntPtr ptr = Marshal.AllocHGlobal(num);
      IntPtr TokenHandle;
      LibWrap.OpenProcessToken(Process.GetCurrentProcess().Handle, 8, ref TokenHandle);
      int ReturnLength;
      LibWrap.GetTokenInformation(TokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, ptr.ToInt32(), num, ref ReturnLength);
      if (num == ReturnLength)
        structure = (TOKEN_ELEVATION) Marshal.PtrToStructure(ptr, typeof (TOKEN_ELEVATION));
      LibWrap.CloseHandle(TokenHandle);
      return structure.TokenIsElevated == 2;
    }

    [STAThread]
    public static void Main(string[] args)
    {
      if ((args.Length == 0 || Operators.CompareString(args[0], "2", false) != 0) && (modMain.ISVista() && !modMain.IsElevated()))
      {
        new Process()
        {
          StartInfo = new ProcessStartInfo()
          {
            UseShellExecute = false,
            LoadUserProfile = false,
            Verb = "runas",
            WorkingDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName),
            FileName = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName),
            Arguments = "2",
            UseShellExecute = true,
            WindowStyle = ProcessWindowStyle.Hidden
          }
        }.Start();
      }
      else
      {
        string str1 = "%windir%\\LTSvc";
        string str2 = "LTService";
        string Default1 = "LabTech";
        string Default2 = "%windir%\\Temp";
        int Default3 = 42000;
        string str3 = "LabVNC";
        try
        {
          Dictionary<string, string> appendedInformation = modMain.CheckAndParseAppendedInformation();
          if (appendedInformation != null)
          {
            if (appendedInformation.ContainsKey("BasePath"))
              str1 = appendedInformation["BasePath"];
            if (appendedInformation.ContainsKey("ServiceName"))
              str2 = appendedInformation["ServiceName"];
            if (appendedInformation.ContainsKey("MainRegkey"))
              Default1 = appendedInformation["MainRegkey"];
            if (appendedInformation.ContainsKey("DefaultTempPath"))
              Default2 = appendedInformation["DefaultTempPath"];
            if (appendedInformation.ContainsKey("TrayPort"))
              Default3 = int.Parse(appendedInformation["TrayPort"]);
            if (appendedInformation.ContainsKey("VNCServiceName"))
              str3 = appendedInformation["VNCServiceName"];
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          modMain.LogEvent(string.Format("Error while setting appended information: {0}", (object) ex.Message));
          ProjectData.ClearProjectError();
        }
        try
        {
          if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Update.ini"))
          {
            modMain.LogEvent("Found update.ini file.");
            IniFile iniFile = new IniFile(Environment.ExpandEnvironmentVariables(AppDomain.CurrentDomain.BaseDirectory + "Update.ini"));
            str1 = iniFile.GetString("CUSTOM", "BasePath", str1);
            str2 = iniFile.GetString("CUSTOM", "ServiceName", str2);
            Default1 = iniFile.GetString("CUSTOM", "MainRegkey", Default1);
            Default2 = iniFile.GetString("CUSTOM", "DefaultTempPath", Default2);
            Default3 = iniFile.GetInteger("CUSTOM", "TrayPort", Default3);
            str3 = iniFile.GetString("CUSTOM", "VNCServiceName", str3);
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          modMain.LogEvent(string.Format("Error while reading update.ini file: {0}", (object) ex.Message));
          ProjectData.ClearProjectError();
        }
        string str4 = Operators.CompareString(str2, "LTService", false) == 0 ? "LTSvcMon" : string.Format("{0}mon", (object) str2);
        TimeSpan timeSpan;
        try
        {
          using (ServiceController serviceController1 = new ServiceController(str2))
          {
            if (serviceController1.Status == ServiceControllerStatus.Running)
            {
              serviceController1.ExecuteCommand(141);
              serviceController1.ExecuteCommand(142);
              serviceController1.Stop();
              ServiceController serviceController2 = serviceController1;
              int num = 1;
              timeSpan = new TimeSpan(0, 0, 30);
              TimeSpan timeout = timeSpan;
              serviceController2.WaitForStatus((ServiceControllerStatus) num, timeout);
            }
            serviceController1.Close();
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
        }
        try
        {
          using (ServiceController serviceController1 = new ServiceController(str4))
          {
            if (serviceController1.Status == ServiceControllerStatus.Running)
            {
              serviceController1.Stop();
              ServiceController serviceController2 = serviceController1;
              int num = 1;
              timeSpan = new TimeSpan(0, 0, 30);
              TimeSpan timeout = timeSpan;
              serviceController2.WaitForStatus((ServiceControllerStatus) num, timeout);
            }
            serviceController1.Close();
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
        }
        try
        {
          using (ServiceController serviceController1 = new ServiceController(str3))
          {
            if (serviceController1.Status == ServiceControllerStatus.Running)
            {
              serviceController1.Stop();
              ServiceController serviceController2 = serviceController1;
              int num = 1;
              timeSpan = new TimeSpan(0, 0, 30);
              TimeSpan timeout = timeSpan;
              serviceController2.WaitForStatus((ServiceControllerStatus) num, timeout);
            }
            serviceController1.Close();
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
        }
        Thread.Sleep(1000);
        if (File.Exists(Environment.ExpandEnvironmentVariables(str1 + "\\LabVNC.exe")))
        {
          try
          {
            modMain.RunProcess(Environment.ExpandEnvironmentVariables(str1 + "\\LabVNC.exe"), Environment.ExpandEnvironmentVariables("-Remove " + str3));
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
        }
        if (File.Exists(Environment.ExpandEnvironmentVariables(str1 + "\\tvnserver.exe")))
        {
          try
          {
            modMain.RunProcess(Environment.ExpandEnvironmentVariables(str1 + "\\tvnserver.exe"), Environment.ExpandEnvironmentVariables(" -stop -silent"));
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            modMain.RunProcess(Environment.ExpandEnvironmentVariables(str1 + "\\tvnserver.exe"), Environment.ExpandEnvironmentVariables(" -remove -silent"));
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
        }
        string str5 = Environment.ExpandEnvironmentVariables(str1);
        try
        {
          Process[] processes = Process.GetProcesses();
          int index = 0;
          while (index < processes.Length)
          {
            Process process = processes[index];
            try
            {
              if (Operators.CompareString(Path.GetDirectoryName(process.MainModule.FileName).ToLower(), str5.ToLower(), false) == 0)
                process.Kill();
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            checked { ++index; }
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          modMain.LogEvent("GetProccess " + ex.Message);
          ProjectData.ClearProjectError();
        }
        Thread.Sleep(5000);
        modMain.RemoveLabTechService(str5, "LTSVC.exe", str2, modMain.ServiceType.LTService);
        modMain.RemoveLabTechService(str5, "LTSVCMon.exe", "LTServiceMon", modMain.ServiceType.LTServiceMon);
        modMain.RemoveLabTechService(str5, "LTSVCMon.exe", str4, modMain.ServiceType.LTServiceMon);
        Thread.Sleep(5000);
        try
        {
          if (File.Exists(Environment.ExpandEnvironmentVariables(str5 + "\\wodVpn.dll")))
            modMain.RunProcess(Environment.ExpandEnvironmentVariables("%windir%\\System32\\Regsvr32.exe"), Environment.ExpandEnvironmentVariables(" /u /s " + Environment.ExpandEnvironmentVariables(str5 + "\\wodVpn.dll")));
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
        }
        try
        {
          string[] strArray = new string[2]
          {
            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\",
            "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
          };
          int index1 = 0;
          while (index1 < strArray.Length)
          {
            string name1 = strArray[index1];
            using (RegistryKey registryKey1 = Registry.LocalMachine.OpenSubKey(name1, false))
            {
              string[] subKeyNames = registryKey1.GetSubKeyNames();
              int index2 = 0;
              while (index2 < subKeyNames.Length)
              {
                string name2 = subKeyNames[index2];
                using (RegistryKey registryKey2 = registryKey1.OpenSubKey(name2))
                {
                  if (!registryKey2.GetValue("OldDisplayName", (object) "").ToString().StartsWith("LabTech Agent Service"))
                  {
                    if (!registryKey2.GetValue("DisplayName", (object) "").ToString().StartsWith("LabTech Agent Service"))
                    {
                      if (!registryKey2.GetValue("DisplayName", (object) "").ToString().StartsWith("LabTech Software Remote Agent"))
                      {
                        if (!registryKey2.GetValue("DisplayName", (object) "").ToString().StartsWith("LabTech® Software Remote Agent"))
                          goto label_76;
                      }
                    }
                  }
                  try
                  {
                    modMain.RunProcess(registryKey2.GetValue("UninstallString", (object) "").ToString(), "/quiet");
                  }
                  catch (Exception ex)
                  {
                    ProjectData.SetProjectError(ex);
                    ProjectData.ClearProjectError();
                  }
label_76:
                  registryKey2.Close();
                }
                checked { ++index2; }
              }
            }
            checked { ++index1; }
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
        }
        try
        {
          if (Directory.Exists(Environment.ExpandEnvironmentVariables(str5)))
            Directory.Delete(Environment.ExpandEnvironmentVariables(str5), true);
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
        }
        if (Directory.Exists(str5))
        {
          Thread.Sleep(5000);
          try
          {
            Process[] processes = Process.GetProcesses();
            int index = 0;
            while (index < processes.Length)
            {
              Process process = processes[index];
              try
              {
                if (Operators.CompareString(Path.GetDirectoryName(process.MainModule.FileName).ToLower(), str5.ToLower(), false) == 0)
                  process.Kill();
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              checked { ++index; }
            }
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            modMain.LogEvent("Kill Proccess " + ex.Message);
            ProjectData.ClearProjectError();
          }
          try
          {
            string[] directories = Directory.GetDirectories(Environment.ExpandEnvironmentVariables(str5));
            int index = 0;
            while (index < directories.Length)
            {
              string path = directories[index];
              try
              {
                Directory.Delete(path, true);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              checked { ++index; }
            }
            try
            {
              if (Directory.Exists(Environment.ExpandEnvironmentVariables(str5)))
                Directory.Delete(Environment.ExpandEnvironmentVariables(str5), true);
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          if (Directory.Exists(str5))
          {
            Thread.Sleep(5000);
            try
            {
              Process[] processes = Process.GetProcesses();
              int index = 0;
              while (index < processes.Length)
              {
                Process process = processes[index];
                try
                {
                  if (Operators.CompareString(Path.GetDirectoryName(process.MainModule.FileName).ToLower(), str5.ToLower(), false) == 0)
                    process.Kill();
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
                checked { ++index; }
              }
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              modMain.LogEvent("Kill Proccess " + ex.Message);
              ProjectData.ClearProjectError();
            }
            try
            {
              if (Directory.Exists(Environment.ExpandEnvironmentVariables(str5)))
                Directory.Delete(Environment.ExpandEnvironmentVariables(str5), true);
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
          }
        }
        try
        {
          RegistryKey registryKey1 = Registry.LocalMachine.OpenSubKey(string.Format("Software\\{0}\\", (object) Default1), true);
          if (registryKey1 != null)
          {
            try
            {
              registryKey1.DeleteSubKeyTree(str2.Substring(2));
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey1.DeleteSubKeyTree("LabVNC");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey1.DeleteSubKeyTree("Tray");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey1.DeleteSubKeyTree("Probe");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey1.DeleteSubKeyTree("ProbeService");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey1.DeleteSubKeyTree("Plugins");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            registryKey1.Close();
          }
          RegistryKey registryKey2 = Registry.LocalMachine.OpenSubKey(string.Format("Software\\Wow6432Node\\{0}\\", (object) Default1), true);
          if (registryKey2 != null)
          {
            try
            {
              registryKey2.DeleteSubKeyTree(str2.Substring(2));
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey2.DeleteSubKeyTree("LabVNC");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey2.DeleteSubKeyTree("Tray");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey2.DeleteSubKeyTree("Probe");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey2.DeleteSubKeyTree("ProbeService");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            try
            {
              registryKey2.DeleteSubKeyTree("Plugins");
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
            registryKey2.Close();
          }
          try
          {
            modMain.RemoveInstallerInfo();
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          if (Operators.CompareString(str2, "LTService", false) == 0)
          {
            List<string> guidList = new List<string>();
            KeyValuePair<RegistryKey, string>[] keyValuePairArray = new KeyValuePair<RegistryKey, string>[3]
            {
              new KeyValuePair<RegistryKey, string>(Registry.LocalMachine, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"),
              new KeyValuePair<RegistryKey, string>(Registry.LocalMachine, "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"),
              new KeyValuePair<RegistryKey, string>(Registry.ClassesRoot, "Installer\\Dependencies\\")
            };
            int index1 = 0;
            while (index1 < keyValuePairArray.Length)
            {
              KeyValuePair<RegistryKey, string> keyValuePair = keyValuePairArray[index1];
              RegistryKey registryKey3 = keyValuePair.Key.OpenSubKey(keyValuePair.Value, true);
              if (registryKey3 != null)
              {
                try
                {
                  registryKey3.DeleteSubKeyTree("{206A1CA5-EB86-400E-8B60-B27869F18501}");
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
                try
                {
                  registryKey3.DeleteSubKeyTree("{3F460D4C-D217-46B4-80B6-B5ED50BD7CF5}");
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
                try
                {
                  registryKey3.DeleteSubKeyTree("{04dec523-746f-4527-b75f-141e3cb0d0aa}");
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
                try
                {
                  registryKey3.DeleteSubKeyTree("{a7e919b0-ab60-4ba7-a4e5-e1c87e23a5bf}");
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
                registryKey3.Close();
              }
              try
              {
                RegistryKey registryKey4 = keyValuePair.Key.OpenSubKey(keyValuePair.Value, true);
                string[] subKeyNames = registryKey4.GetSubKeyNames();
                int index2 = 0;
                while (index2 < subKeyNames.Length)
                {
                  string name = subKeyNames[index2];
                  RegistryKey registryKey5 = registryKey4.OpenSubKey(name);
                  if (registryKey5.GetValue("OldDisplayName", (object) "").ToString().StartsWith("LabTech Agent Service") || registryKey5.GetValue("DisplayName", (object) "").ToString().StartsWith("LabTech Agent Service") || (registryKey5.GetValue("DisplayName", (object) "").ToString().StartsWith("LabTech Software Remote Agent") || registryKey5.GetValue("DisplayName", (object) "").ToString().StartsWith("LabTech® Software Remote Agent")))
                    guidList.Add(name);
                  registryKey5.Close();
                  checked { ++index2; }
                }
                registryKey4.Close();
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              checked { ++index1; }
            }
            modMain.DeleteKeysFromUnInstallAndDependencies(guidList);
            try
            {
              if (Directory.Exists(Environment.ExpandEnvironmentVariables("%ALLUSERSPROFILE%\\Labtech\\")))
                Directory.Delete(Environment.ExpandEnvironmentVariables("%ALLUSERSPROFILE%\\Labtech\\"), true);
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              modMain.LogEvent("Del Dir " + ex.Message);
              ProjectData.ClearProjectError();
            }
            try
            {
              if (File.Exists(Environment.ExpandEnvironmentVariables("%ALLUSERSPROFILE%\\Start Menu\\Programs\\Startup\\Network Monitoring Tray.lnk")))
                File.Delete(Environment.ExpandEnvironmentVariables("%ALLUSERSPROFILE%\\Start Menu\\Programs\\Startup\\Network Monitoring Tray.lnk"));
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              modMain.LogEvent("Del Dir " + ex.Message);
              ProjectData.ClearProjectError();
            }
          }
          try
          {
            if (Directory.Exists(Environment.ExpandEnvironmentVariables(Default2 + "\\LTCache")))
              Directory.Delete(Environment.ExpandEnvironmentVariables(Default2 + "\\LTCache"), true);
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            modMain.LogEvent("Del Dir " + ex.Message);
            ProjectData.ClearProjectError();
          }
          try
          {
            if (Directory.Exists(Environment.ExpandEnvironmentVariables(Default2 + "\\LTUpdate")))
              Directory.Delete(Environment.ExpandEnvironmentVariables(Default2 + "\\LTUpdate"), true);
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            modMain.LogEvent("Del Dir " + ex.Message);
            ProjectData.ClearProjectError();
          }
          try
          {
            if (Directory.Exists(Environment.ExpandEnvironmentVariables(Default2 + "\\LTApp")))
              Directory.Delete(Environment.ExpandEnvironmentVariables(Default2 + "\\LTApp"), true);
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            modMain.LogEvent("Del Dir " + ex.Message);
            ProjectData.ClearProjectError();
          }
          try
          {
            RegistryKey subKey1 = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Policies\\Microsoft\\Windows\\WindowsUpdate");
            if (subKey1 != null)
            {
              try
              {
                subKey1.DeleteValue("ElevateNonAdmins", false);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              try
              {
                subKey1.DeleteValue("TargetGroup", false);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              try
              {
                subKey1.DeleteValue("TargetGroupEnabled", false);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              try
              {
                subKey1.DeleteValue("WUServer", false);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              try
              {
                subKey1.DeleteValue("WUStatusServer", false);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              try
              {
                subKey1.DeleteValue("DisableWindowsUpdateAccess", false);
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              RegistryKey subKey2 = subKey1.CreateSubKey("AU");
              subKey2.SetValue("NoAutoUpdate", (object) 0);
              subKey2.SetValue("UseWUServer", (object) 0);
              subKey2.SetValue("AUOptions", (object) "2");
              subKey2.SetValue("NoAutoRebootWithLoggedOnUsers", (object) 1);
              subKey2.Close();
              try
              {
                RegistryKey subKey3 = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Security Center");
                subKey3.SetValue("UpdatesDisableNotify", (object) 0);
                subKey3.Close();
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
              try
              {
                RegistryKey subKey3 = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
                subKey3.DeleteValue("HideSCAHealth", false);
                subKey3.Close();
              }
              catch (Exception ex)
              {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
              }
            }
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"Allow NetFasTalk\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"Allow Local VNC\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"Allow Local Redir\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"Allow Tunnel StunRelay\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"Allow Tunnel\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"AgentService\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"AgentMonitor\"");
            modMain.RunProcess("CMD.exe", "/c netsh advfirewall firewall Delete rule name=\"AgentTray\"");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening udp " + Conversions.ToString(Default3) + " ");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening udp " + Conversions.ToString(checked (Default3 + 1)) + " ");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening udp " + Conversions.ToString(checked (Default3 + 2)) + " ");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening udp " + Conversions.ToString(checked (Default3 + 3)) + " ");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening udp " + Conversions.ToString(checked (Default3 + 4)) + " ");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening udp 162");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening tcp 4999");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening tcp 4998");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening tcp 4997");
            modMain.RunProcess("CMD.exe", "/c netsh firewall delete portopening tcp 4996");
            modMain.RunProcess("CMD.exe", string.Format("/c netsh firewall delete allowedprogram {0}\\LTSVC.exe ", (object) str5));
            modMain.RunProcess("CMD.exe", string.Format("/c netsh firewall delete allowedprogram {0}\\LTSVCmon.exe", (object) str5));
            modMain.RunProcess("CMD.exe", string.Format("/c netsh firewall delete allowedprogram {0}\\LTTray.exe", (object) str5));
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.CurrentUser.DeleteSubKeyTree("Software\\LabTech");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Network\\" + str2);
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Network\\LabVNC");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\LabTech Tray Icon");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Network\\tvnserver");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("Software\\Sysinternals");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("Software\\Wow6432Node\\Sysinternals");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("Software\\UltraVNC");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("Software\\ORL");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Tracing\\LabtechRemoteAgentAutoInstall_RASAPI32");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Tracing\\LabtechRemoteAgentAutoInstall_RASMANCS");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Tracing\\LTSVC_RASAPI32");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Tracing\\LTSVC_RASMANCS");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Tracing\\LTvcMon_RASAPI32");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Registry.LocalMachine.DeleteSubKeyTree("SOFTWARE\\Microsoft\\Tracing\\LTSvcMon_RASMANCS");
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          try
          {
            Directory.Delete(Environment.ExpandEnvironmentVariables(Default2 + "\\_LTUpdate"), true);
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
        }
        catch (Exception ex)
        {
          ProjectData.SetProjectError(ex);
          modMain.LogEvent("RegKey " + ex.Message);
          ProjectData.ClearProjectError();
        }
      }
    }

    public static void RemoveInstallerInfo()
    {
      using (RegistryKey registryKey1 = Registry.ClassesRoot.OpenSubKey("Installer\\Products", true))
      {
        string[] subKeyNames1 = registryKey1.GetSubKeyNames();
        int index1 = 0;
        while (index1 < subKeyNames1.Length)
        {
          string str1 = subKeyNames1[index1];
          bool flag1 = false;
          string str2 = (string) null;
          string path = (string) null;
          try
          {
            using (RegistryKey registryKey2 = registryKey1.OpenSubKey(str1))
            {
              string str3 = registryKey2.GetValue("ProductName", (object) string.Empty).ToString();
              flag1 = modMain.RemoteAgentIdentifiers.Contains(str3);
              if (flag1)
              {
                str2 = registryKey2.GetValue("PackageCode", (object) string.Empty).ToString();
                using (RegistryKey registryKey3 = registryKey2.OpenSubKey("SourceList"))
                {
                  string str4 = registryKey3.GetValue("LastUsedSource", (object) string.Empty).ToString();
                  path = Path.Combine(str4.Substring(str4.LastIndexOf(';')), registryKey3.GetValue("PackageName", (object) string.Empty).ToString());
                }
              }
            }
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          if (flag1)
          {
            try
            {
              using (RegistryKey registryKey2 = Registry.ClassesRoot.OpenSubKey("Installer\\UpgradeCodes", true))
              {
                string[] subKeyNames2 = registryKey2.GetSubKeyNames();
                int index2 = 0;
                while (index2 < subKeyNames2.Length)
                {
                  string str3 = subKeyNames2[index2];
                  bool flag2 = false;
                  using (RegistryKey registryKey3 = registryKey2.OpenSubKey(str3))
                    flag2 = new List<string>((IEnumerable<string>) registryKey3.GetValueNames()).Contains(str1);
                  if (flag2)
                  {
                    try
                    {
                      registryKey2.DeleteSubKeyTree(str3);
                    }
                    catch (Exception ex)
                    {
                      ProjectData.SetProjectError(ex);
                      ProjectData.ClearProjectError();
                    }
                  }
                  checked { ++index2; }
                }
              }
              registryKey1.DeleteSubKeyTree(str1);
              if (File.Exists(path))
              {
                try
                {
                  File.Delete(path);
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
              }
            }
            catch (Exception ex)
            {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
            }
          }
          checked { ++index1; }
        }
      }
      using (RegistryKey registryKey1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData", true))
      {
        string[] subKeyNames1 = registryKey1.GetSubKeyNames();
        int index1 = 0;
        while (index1 < subKeyNames1.Length)
        {
          string str1 = subKeyNames1[index1];
          try
          {
            using (RegistryKey registryKey2 = registryKey1.OpenSubKey(string.Format("{0}\\Products", (object) str1), true))
            {
              string[] subKeyNames2 = registryKey2.GetSubKeyNames();
              int index2 = 0;
              while (index2 < subKeyNames2.Length)
              {
                string subkey = subKeyNames2[index2];
                bool flag = false;
                try
                {
                  using (RegistryKey registryKey3 = registryKey2.OpenSubKey(string.Format("{0}\\InstallProperties", (object) subkey)))
                  {
                    string str2 = registryKey3.GetValue("DisplayName", (object) string.Empty).ToString();
                    flag = modMain.RemoteAgentIdentifiers.Contains(str2);
                  }
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
                if (flag)
                {
                  try
                  {
                    registryKey2.DeleteSubKeyTree(subkey);
                  }
                  catch (Exception ex)
                  {
                    ProjectData.SetProjectError(ex);
                    ProjectData.ClearProjectError();
                  }
                }
                checked { ++index2; }
              }
            }
          }
          catch (Exception ex)
          {
            ProjectData.SetProjectError(ex);
            ProjectData.ClearProjectError();
          }
          checked { ++index1; }
        }
      }
    }

    internal static bool RunProcess(string ProcessName, string ProcessArgs)
    {
      try
      {
        Process.Start(new ProcessStartInfo(ProcessName, ProcessArgs)
        {
          CreateNoWindow = true,
          UseShellExecute = true,
          WindowStyle = ProcessWindowStyle.Hidden
        }).WaitForExit();
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        ProjectData.ClearProjectError();
      }
      return true;
    }

    public static void LogEvent(string message)
    {
      try
      {
        StreamWriter streamWriter = new StreamWriter(Environment.ExpandEnvironmentVariables("%Windir%\\TEMP\\LTErrors.txt"), true);
        streamWriter.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + "  v" + Conversions.ToString(Assembly.GetExecutingAssembly().GetName().Version.Major) + "." + Conversions.ToString(Assembly.GetExecutingAssembly().GetName().Version.Minor) + "\t - " + Conversions.ToString(DateAndTime.Now) + "\t - " + message + ":::");
        streamWriter.Close();
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        ProjectData.ClearProjectError();
      }
    }

    internal static bool KillProcess(string ProcessName)
    {
      bool flag;
      try
      {
        if (ProcessName.IndexOf(".") != -1)
          ProcessName = ProcessName.Substring(0, ProcessName.IndexOf("."));
        Process[] processesByName1 = Process.GetProcessesByName(ProcessName.ToLower());
        int index1 = 0;
        while (index1 < processesByName1.Length)
        {
          Process process = processesByName1[index1];
          if (!process.CloseMainWindow())
            process.Kill();
          checked { ++index1; }
        }
        Process[] processesByName2 = Process.GetProcessesByName(ProcessName);
        int index2 = 0;
        while (index2 < processesByName2.Length)
        {
          processesByName2[index2].Kill();
          checked { ++index2; }
        }
        flag = true;
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        modMain.LogEvent("KillProcess " + ex.Message);
        flag = false;
        ProjectData.ClearProjectError();
      }
      return flag;
    }

    internal static void DeleteKeysFromUnInstallAndDependencies(List<string> guidList)
    {
      KeyValuePair<RegistryKey, string>[] keyValuePairArray = new KeyValuePair<RegistryKey, string>[3]
      {
        new KeyValuePair<RegistryKey, string>(Registry.LocalMachine, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"),
        new KeyValuePair<RegistryKey, string>(Registry.LocalMachine, "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"),
        new KeyValuePair<RegistryKey, string>(Registry.ClassesRoot, "Installer\\Dependencies\\")
      };
      int index = 0;
      while (index < keyValuePairArray.Length)
      {
        KeyValuePair<RegistryKey, string> keyValuePair = keyValuePairArray[index];
        List<string>.Enumerator enumerator;
        using (RegistryKey registryKey = keyValuePair.Key.OpenSubKey(keyValuePair.Value, true))
        {
          if (registryKey != null)
          {
            try
            {
              enumerator = guidList.GetEnumerator();
              while (enumerator.MoveNext())
              {
                string current = enumerator.Current;
                try
                {
                  registryKey.DeleteSubKeyTree(current);
                }
                catch (Exception ex)
                {
                  ProjectData.SetProjectError(ex);
                  ProjectData.ClearProjectError();
                }
              }
            }
            finally
            {
              enumerator.Dispose();
            }
          }
        }
        checked { ++index; }
      }
      guidList.Clear();
    }

    public static void RemoveLabTechService(string basePath, string file, string serviceName, modMain.ServiceType _serviceType)
    {
      string str = _serviceType == modMain.ServiceType.LTService ? "name" : "ServiceName";
      modMain.LogEvent("Removing Service Monitor");
      try
      {
        if (File.Exists(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework64\\v2.0.50727\\installutil.exe")))
          modMain.RunProcess(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework64\\v2.0.50727\\installutil.exe"), Environment.ExpandEnvironmentVariables(string.Format("/{0}={1} /u {2}\\{3}", new object[4]
          {
            (object) str,
            (object) serviceName,
            (object) basePath,
            (object) file
          })));
        else if (File.Exists(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework\\v2.0.50727\\installutil.exe")))
          modMain.RunProcess(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework\\v2.0.50727\\installutil.exe"), Environment.ExpandEnvironmentVariables(string.Format("/{0}={1} /u {2}\\{3}", new object[4]
          {
            (object) str,
            (object) serviceName,
            (object) basePath,
            (object) file
          })));
        else if (File.Exists(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe")))
        {
          modMain.RunProcess(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework64\\v4.0.30319\\installutil.exe"), Environment.ExpandEnvironmentVariables(string.Format("/{0}={1} /u {2}\\{3}", new object[4]
          {
            (object) str,
            (object) serviceName,
            (object) basePath,
            (object) file
          })));
        }
        else
        {
          if (!File.Exists(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe")))
            return;
          modMain.RunProcess(Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe"), Environment.ExpandEnvironmentVariables(string.Format("/{0}={1} /u {2}\\{3}", new object[4]
          {
            (object) str,
            (object) serviceName,
            (object) basePath,
            (object) file
          })));
        }
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        Exception exception = ex;
        modMain.LogEvent(string.Format("Remove {0} update. Ex: {1}", (object) serviceName, (object) exception.Message));
        ProjectData.ClearProjectError();
      }
    }

    public static Dictionary<string, string> CheckAndParseAppendedInformation()
    {
      Dictionary<string, string> dictionary = new Dictionary<string, string>();
      try
      {
        string path = string.Format("{0}\\Agent_Uninstall.exe", (object) Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
        modMain.LogEvent(string.Format("Looking for appended information in file: {0}", (object) path));
        if (!File.Exists(path))
        {
          modMain.LogEvent(string.Format("Could not find file: {0}", (object) path));
          return (Dictionary<string, string>) null;
        }
        modMain.LogEvent("Found file, looking for appended info.");
        FileAppendedInformation appendedInformation1 = new FileAppendedInformation(File.ReadAllBytes(path));
        if ((long) appendedInformation1.GetDataCount() <= 0L)
        {
          modMain.LogEvent("No appended information found attached to file.");
          return (Dictionary<string, string>) null;
        }
        byte[][] appendedInformation2 = appendedInformation1.GetAppendedInformation();
        int index = 0;
        while (index < appendedInformation2.Length)
        {
          string str1 = Encoding.Unicode.GetString(appendedInformation2[index]);
          string key = str1.Substring(0, str1.IndexOf('='));
          string str2 = str1.Substring(checked (str1.IndexOf('=') + 1)).Trim().Replace("\0", "");
          dictionary.Add(key, str2);
          modMain.LogEvent(string.Format("Found appended information: {0}={1}", (object) key, (object) str2));
          checked { ++index; }
        }
      }
      catch (Exception ex)
      {
        ProjectData.SetProjectError(ex);
        modMain.LogEvent(string.Format("Error getting appended information: {0}", (object) ex.Message));
        ProjectData.ClearProjectError();
      }
      return dictionary;
    }

    public enum ServiceType
    {
      LTService = 1,
      LTServiceMon = 2,
    }
  }
}