rust command::new()

CrossPython / 2023-08-26 / 原文

use std::process::Command;

fn  is_program_running(program: &str) ->bool {
    let cmdstr = format!("IMAGENAME eq {}", program);

    let output = Command::new("cmd")
                    .arg("/S")
                    .arg("/c")
                    .arg("tasklist")
                    .arg("/FI")
                    .arg(cmdstr)
                    .output()
                    .expect("-1");
let output_str = String::from_utf8_lossy(&output.stdout);
let result = match output_str.find(program) {
    Some(_) => true,
    None => false
};
result
}

fn main(){
    let s = is_program_running("notepad.exe");
    println!("{:?}", s);
}