简体   繁体   English

如何在Java中运行.reg文件

[英]How to run .reg file in Java

I need to install a .reg file (INTRANET) by using Java. 我需要使用Java安装.reg文件(INTRANET)。 How do i get my goal ? 我如何实现目标?

Cheers, 干杯,

You could use System.exec to launch regedit yourfile.reg 您可以使用System.exec启动regedit yourfile.reg

Here is how to do it : 这是怎么做的:

String[] cmd = {"regedit", "yourfile.reg"};
Process p = Runtime.exec(cmd);
p.waitFor();

Last line is optional, it only allows you to wait until the operation is over. 最后一行是可选的,它只允许您等到操作结束。

If you're already on Java 1.6, just grab java.awt.Desktop : 如果您已经使用Java 1.6,只需抓住java.awt.Desktop

Desktop.getDesktop().open(new File("c:/yourfile.reg"));

It will launch the file using the default application associated with it, as if you're doubleclicking the particular file in Windows explorer. 它将使用与其关联的默认应用程序启动文件,就像您在Windows资源管理器中双击特定文件一样。

This can achieved through Process Builder in JAVA. 这可以通过JAVA中的Process Builder实现。 Please consider the following example for this: 请考虑以下示例:

ProcessBuilder processBuilder = new ProcessBuilder("regedit", "reg_file_to_run.reg");
Process processToExecute = processBuilder.start();

And then you can optionally wait for the completion of process execution with this line: 然后您可以选择等待此行完成流程执行:

processToExecute.waitFor();

Note: If command in your registry file asks for confirmation prompts while making changes in registry entries, you can perform it silently as well with '/s' option. 注意:如果注册表文件中的命令在注册表项中进行更改时要求提供确认提示,则可以使用“/ s”选项以静默方式执行该命令。 Like this: 像这样:

ProcessBuilder processBuilder = new ProcessBuilder("regedit", "/s", "reg_file_to_run.reg");

With this command would be executed silently without any confirmation prompt. 使用此命令将在没有任何确认提示的情况下静默执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM