如何在 Java Netbeans 代码中运行 reg 文件 [英] How to run reg file in Java Netbeans Code
问题描述
请帮我解决:如何在java netbeans中运行.reg文件?如果我在不同的计算机上打开程序,如何自动设置源文件?
Please help me to solve : how to run .reg file in java netbeans ? And how to set source file automatically if I open the program in different computer ?
我有一个像这样的 reg 文件:
I have an reg file like this :
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR]
"Start"=dword:00000004
这是我的 Java 代码:
And this is my Java Code :
/**
* Creates new form Form_DisableEnable
*/
public Form_DisableEnable() {
initComponents();
Dimension x = Toolkit.getDefaultToolkit().getScreenSize();
Dimension y = getSize();
setLocation(
(x.width - y.width)/2,
(x.height - y.height)/2);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Disable/Enable USB");
jButton1.setText("Disable");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Enable");
jLabel1.setText("IP Address");
jMenu1.setText("File");
jMenuItem2.setText("Sign out");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Helps");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2))
.addComponent(jTextField1)))
.addContainerGap(213, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(168, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(JOptionPane.showConfirmDialog(null,"Are You sure want to logout ? ","Caution",
JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE)==JOptionPane.OK_OPTION) {
LoginForm dexs=new LoginForm();
dexs.setVisible(true);
this.dispose();
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Form_DisableEnable().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JTextField jTextField1;
// End of variables declaration}
提前致谢.问候,大卫
推荐答案
试试这个导入注册表文件:
Try this to import registry file:
Desktop.getDesktop().open(new File("c:/file.reg"));
如果文件在 jar 中,那么您可能需要将其写入临时文件并打开该文件.
If the file is in a jar, then you would probably need to write it to a temporary file and open that file instead.
尝试类似于以下代码段的内容加载注册表文件,将其复制到临时文件,然后使用默认注册表编辑器打开临时文件:
Try something similar to the following snippet to load registry file, copy it to a temp file and then open temp file using default registry editor:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
loadRegFile("/resource/disable.reg");
}
public void loadRegFile(String filePath){
try {
//create temp file
File temp = File.createTempFile("temp", ".reg");
temp.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
//load an appropriate registry file
InputStream in = getClass().getResourceAsStream(filePath);
//copy to temp file
IOUtils.copy(in,out);
out.close();
//open using regedit
Desktop.getDesktop().open(temp);
} catch (IOException ex) {
ex.printStackTrace();
}
}
这篇关于如何在 Java Netbeans 代码中运行 reg 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!