4

我想知道 Java 中的代码,这将有助于执行与在任何操作系统中双击打开文件相同的操作,从而使我们能够在用户提供位置时查看其内容文件在他/她的 PC 中。任何建议都会有很大帮助,因为我需要它来完成我的申请。

4

3 回答 3

5

请参阅Desktop.open(文件)

于 2011-10-20T18:20:26.933 回答
2

我使用了以下代码。在 Windows 上,如果没有程序与文件类型关联,您将获得一个 Windows 文件打开对话框。如果它不在 Windows 上运行,它会回退到Desktop.open(),如果系统知道文件类型,它会起作用。

public static boolean openFile(File file){
  try {
    if (System.getProperty("os.name").contains("Windows")) {
      Runtime.getRuntime().exec(
          "rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath());
    } else if (Desktop.isDesktopSupported()) {
      Desktop.getDesktop().open(file);
    } else {
      return false;
    }
    return true;
  } catch (final Exception e1) {
    System.err.println("Error opening file " + file, e1);
    return false;
  }
}
于 2011-10-20T18:28:05.377 回答
0

在 Windows XP 中:

rundll32 shell32.dll,ShellExec_RunDLL "file:///d:\download\theapp.lnk"

您可以添加注册表,以便您可以在运行对话框和文件夹巡洋舰中运行 lnk 文件:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\lnkfile\shell]

[HKEY_CLASSES_ROOT\lnkfile\shell\edit]

[HKEY_CLASSES_ROOT\lnkfile\shell\edit\command]
@="rundll32 shell32.dll,ShellExec_RunDLL \"file:///%1\""
于 2012-09-10T16:19:35.247 回答