简体   繁体   English

如何在Java Netbeans代码中运行reg文件

[英]How to run reg file in Java Netbeans Code

Please help me to solve : how to run .reg file in java netbeans ? 请帮我解决:如何在java netbeans中运行.reg文件? And how to set source file automatically if I open the program in different computer ? 如果在其他计算机上打开程序,如何自动设置源文件?

I have an reg file like this : 我有一个这样的reg文件:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR]
"Start"=dword:00000004

And this is my Java Code : 这是我的Java代码:

/**
 * 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}

Thanks in advance. 提前致谢。 Regards, David 问候,大卫

Try this to import registry file: 试试这个导入注册表文件:

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

If the file is in a jar, then you would probably need to write it to a temporary file and open that file instead. 如果文件在jar中,则可能需要将其写入临时文件并打开该文件。

EDIT: 编辑:

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();
    }
}

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

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