编程语言
首页 > 编程语言> > java – 防止wmic.exe编写TempWmicBatchFile.bat

java – 防止wmic.exe编写TempWmicBatchFile.bat

作者:互联网

我想在我的Java应用程序中检查Windows虚拟键盘是否已经运行.

我搜索它并发现,我可以使用wmic.exe来搜索进程.

这就是我正在做的事情:

Process proc = Runtime.getRuntime().exec("wmic.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(proc
    .getInputStream()));
OutputStreamWriter oStream = new OutputStreamWriter(proc
    .getOutputStream());
oStream .write("process where name='osk.exe' get caption");
oStream .flush();
oStream .close();
input.readLine();
while ((in = input.readLine()) != null) {
    if (in.contains("osk.exe")) {
        input.close();
        proc.destroy();
        return;
    }
}
input.close();
proc.destroy();

这是有效的,但是wmic以某种方式使用行进程创建文件TempWmicBatchFile.bat,其中name =’osk.exe’获取标题.

我怎样才能防止这种情况发生?

解决方法:

您可以避免打开其他流来传递另一个命令.这就是创建临时bat文件的原因.

使用以下代码.它不会创建临时批处理文件

public class WmicTest {

    public static void main(String[] args) throws IOException {

        Process proc = Runtime.getRuntime().exec("wmic.exe process where name='osk.exe' get caption");
        BufferedReader input = new BufferedReader(new InputStreamReader(proc
                .getInputStream()));
//        OutputStreamWriter oStream = new OutputStreamWriter(proc
//                .getOutputStream());
//        oStream.write("process where name='osk.exe' get caption");
//        oStream.flush();
 //       oStream.close();
        input.readLine();
        String in;
        while ((in = input.readLine()) != null) {
            if (in.contains("osk.exe")) {
                System.out.println("Found");
                input.close();
                proc.destroy();
                return;
            }
        }
        input.close();
        proc.destroy();
    }
}

标签:java,temporary-files,wmic,on-screen-keyboard,virtual-keyboard
来源: https://codeday.me/bug/20190704/1374184.html