rust command::new() 查询程序是否在运行

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);
}

  

posted @ 2023-08-26 19:12  CrossPython  阅读(210)  评论(0编辑  收藏  举报