JAVA操作注册表实现开机启动

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class test {
 public void init(){
  JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("测试开机启动");
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JCheckBox check = new JCheckBox();
        check.setText("开机自动启动");
        check.addItemListener(new ItemListener() {
   public void itemStateChanged(ItemEvent e) {
       JCheckBox cb = (JCheckBox) e.getSource();
       try {
     changeStart(cb.isSelected());
    } catch (IOException e1) {
     e1.printStackTrace();
    }

   }
  });
        frame.getContentPane().add(check);

        //显示窗体
        frame.setSize(200,300);
        frame.setVisible(true);
 }
 /**
  * Reg 参数说明
  * /v       所选项之下要添加或删除的值名
  * /t       RegKey 数据类型(reg_sz字符串)
  * /d       要分配给添加的注册表 ValueName 的数据
  * /f         不用提示就强行删除
  */
 public void changeStart(boolean isStartAtLogon) throws IOException{
  String regKey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  String myAppName = "MyApp";
  String exePath = "D:\\Software\\eclipse\\eclipse.exe";
  Runtime.getRuntime().exec("reg "+(isStartAtLogon?"add ":"delete ")+regKey+" /v "+myAppName+(isStartAtLogon?" /t reg_sz /d "+exePath:" /f"));
 }
 
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new test().init();
            }
        });
    }
} 

你可能感兴趣的:(java,eclipse,windows,swing,Microsoft)