如何使用Win32_ShortcutFile重命名fileName - WMI模式方法重命名和Jacob

时间:2013-09-10 11:39:54

标签: java wmi jacob

作为Jacob新手,我无法使用Win32_ShortcutFile架构的重命名方法来更改文件名。我想通过只调用schema方法来完成它。请建议我该如何继续。

我的代码如下所示:

    public static void main(String[] args) {
    ComThread.InitMTA();
    ActiveXComponent os = null;

    try {
        ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");
                    String newName = "jacobtest";
        Variant instances = wmi.invoke("InstancesOf", "Win32_ShortcutFile");
        Enumeration<Variant> en = new EnumVariant(instances.getDispatch());

        while (en.hasMoreElements()) {
            ActiveXComponent bb = new ActiveXComponent(en.nextElement()
                    .getDispatch());
            String file = bb.getPropertyAsString("FileName");
            System.out.println(file);

            if (file.equals("filename")) {
                os = new ActiveXComponent(en.nextElement                                        ().getDispatch());
                os.invoke("Rename", newName);
            }

        }
    }

    finally {
        ComThread.Release();
    }

}

1 个答案:

答案 0 :(得分:0)

最后我明白了...... :)

重命名文件时出现轻微错误,应如下所示:

  String newName = "D:\\jacobtest.docx";
  if (file.equals("filename")) {
            os = new ActiveXComponent((en.nextElement).getDispatch());
            os.invoke("Rename", newName);
        }