Using Java, How can I get a list of all local users on a windows machine

前端 未结 4 1306
你的背包
你的背包 2020-12-21 18:17

How can I list all the local users configured on a windows machine (Win2000+) using java.
I would prefer doing this with ought using any java 2 com bridges, or any other

4条回答
  •  爱一瞬间的悲伤
    2020-12-21 19:10

    Using Java COM Object, i.e. Jacob:

    public static void EnumerateUsers() {
    
        String query = "SELECT * FROM Win32_UserAccount";
        ActiveXComponent axWMI = new ActiveXComponent("winmgmts:\\");
        Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
        EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
        Dispatch item = null;
        StringBuilder sb = new StringBuilder();
    
        while (enumVariant.hasMoreElements()) {
                item = enumVariant.nextElement().toDispatch();
                sb.append("User: " + Dispatch.call(item, "Name")).toString();
                System.out.println(sb);
                sb.setLength(0);
        } 
    
    }
    

提交回复
热议问题