在这里建立一个Updater的项目目录,实现以下功能:
1、基于rust开发的windows应用程序 2、配置文件存放在Updater程序运行目录所在盘的AppData下的Updater目录中 3、配置文件里有个DebugMode的配置,为True时,启动后显示命令行窗口,否则静默运行。 4、此应用启动后,每五分钟执行一次Upgrade方法,此方法在控制台输出“开始升级”字样
This commit is contained in:
63
Windows/CS/Framework4.0/Updater/src/main.rs
Normal file
63
Windows/CS/Framework4.0/Updater/src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tokio::time;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Config {
|
||||
debug_mode: bool,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
debug_mode: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_config_path() -> PathBuf {
|
||||
let exe_path = std::env::current_exe().expect("Failed to get executable path");
|
||||
let drive = exe_path.parent().unwrap().parent().unwrap();
|
||||
let appdata = drive.join("AppData").join("Updater");
|
||||
fs::create_dir_all(&appdata).expect("Failed to create config directory");
|
||||
appdata.join("config.json")
|
||||
}
|
||||
|
||||
fn load_config() -> Config {
|
||||
let config_path = get_config_path();
|
||||
if config_path.exists() {
|
||||
let content = fs::read_to_string(&config_path).expect("Failed to read config file");
|
||||
serde_json::from_str(&content).expect("Failed to parse config file")
|
||||
} else {
|
||||
let default_config = Config::default();
|
||||
let content = serde_json::to_string_pretty(&default_config).expect("Failed to serialize config");
|
||||
fs::write(&config_path, content).expect("Failed to write config file");
|
||||
default_config
|
||||
}
|
||||
}
|
||||
|
||||
async fn upgrade() {
|
||||
println!("开始升级");
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let config = load_config();
|
||||
|
||||
if !config.debug_mode {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use windows::Win32::System::Console;
|
||||
unsafe {
|
||||
let _ = Console::FreeConsole();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut interval = time::interval(time::Duration::from_secs(300));
|
||||
loop {
|
||||
interval.tick().await;
|
||||
upgrade().await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user