[feat] optimization for getting cs2 path + add Toast for auto path
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use std::process::Command;
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::process::Command;
|
||||
|
||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||
// const DETACHED_PROCESS: u32 = 0x00000008;
|
||||
@@ -21,18 +21,29 @@ pub fn run_steam() -> std::io::Result<std::process::Output> {
|
||||
.output()
|
||||
}
|
||||
|
||||
// FIXME wmic is deprecated
|
||||
|
||||
pub fn get_exe_path(name: &str) -> Result<String, std::io::Error> {
|
||||
let command = format!("/C wmic process where name='{}' get ExecutablePath", name);
|
||||
// [原理]
|
||||
// Powershell 运行 Get-Process name | Select-Object path
|
||||
// 有name.exe运行时返回
|
||||
// Path
|
||||
// ----
|
||||
// 进程路径
|
||||
let command = format!("Get-Process {} | Select-Object path", name);
|
||||
let args = command.split_whitespace().collect::<Vec<&str>>();
|
||||
let output = Command::new("cmd.exe").args(&args).output()?;
|
||||
let output = Command::new("powershell.exe")
|
||||
.args(&args)
|
||||
.creation_flags(CREATE_NO_WINDOW)
|
||||
.output()?;
|
||||
|
||||
let out = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
let out = String::from_utf8_lossy(&output.stdout).to_string();
|
||||
|
||||
if out.contains("ExecutablePath") {
|
||||
if out.contains("Path") {
|
||||
let out = out.trim();
|
||||
let spt: Vec<&str> = out.split("\r\n").collect();
|
||||
if spt.len() >= 2 {
|
||||
return Ok(spt[1].to_string());
|
||||
|
||||
if spt.len() > 2 {
|
||||
return Ok(spt[2].to_string());
|
||||
}
|
||||
}
|
||||
Err(std::io::Error::new(
|
||||
@@ -68,4 +79,12 @@ mod tests {
|
||||
println!("test open path: {}", path);
|
||||
open_path(path).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_exe_path() {
|
||||
let path = get_exe_path("steam").expect("failed");
|
||||
println!("test get steam path: {}", path);
|
||||
|
||||
get_exe_path("not_running").expect("failed");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user