java 注册表操作

来源:互联网 发布:工艺方案优化 编辑:程序博客网 时间:2024/05/17 07:42
/**//* * WriteRegedit.java * * Created on 2008年3月5日, 下午6:16 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package RegeditInfo;import com.ice.jni.registry.*;import java.text.SimpleDateFormat;import java.util.TimerTask;/** *//** * * @author heavens */public class RegeditBase ...{        static SimpleDateFormat shortDateFormat  = new SimpleDateFormat("yyyy-MM-dd");    /** *//** Creates a new instance of test */      //把信息存储到注册表HKEY_LOCAL_MACHINE下的某个节点的某一变量中,有则修改,无则创建    public static boolean setValue(String folder,String subKeyNode,String subKeyName,String subKeyValue)    ...{        try         ...{                 RegistryKey software = Registry.HKEY_LOCAL_MACHINE.openSubKey(folder);            RegistryKey subKey = software.createSubKey(subKeyNode, "");                  subKey.setValue(new RegStringValue(subKey,subKeyName,subKeyValue));            subKey.closeKey();              return true;        }         catch(NoSuchKeyException e)         ...{                  e.printStackTrace();            }         catch(NoSuchValueException e)        ...{            e.printStackTrace();           }        catch(RegistryException e)         ...{                  e.printStackTrace();            }         return false;    }        //删除注册表中某节点下的某个变量    public static boolean deleteValue(String folder,String subKeyNode,String subKeyName)    ...{        try         ...{                  RegistryKey software = Registry.HKEY_LOCAL_MACHINE.openSubKey(folder);            RegistryKey subKey = software.createSubKey(subKeyNode, "");                subKey.deleteValue(subKeyName);            subKey.closeKey();               return true;        }         catch (NoSuchKeyException e)         ...{                  e.printStackTrace();           }        catch(NoSuchValueException e)        ...{            e.printStackTrace();           }        catch (RegistryException e)        ...{                 e.printStackTrace();            }        return false;    }        //删除注册表中某节点下的某节点    public static boolean deleteSubKey(String folder,String subKeyNode)    ...{        try         ...{                  RegistryKey software = Registry.HKEY_LOCAL_MACHINE.openSubKey(folder);            software.deleteSubKey(subKeyNode);            software.closeKey();               return true;        }         catch (NoSuchKeyException e)         ...{                  e.printStackTrace();           }         catch (RegistryException e)        ...{                 e.printStackTrace();            }        return false;    }        //打开注册表项并读出相应的变量名的值      public static String getValue(String folder,String subKeyNode,String subKeyName)    ...{        String value = "";        try         ...{                  RegistryKey software = Registry.HKEY_LOCAL_MACHINE.openSubKey(folder);            RegistryKey subKey = software.openSubKey(subKeyNode);             value = subKey.getStringValue(subKeyName);             subKey.closeKey();          }         catch (NoSuchKeyException e)         ...{                  value = "NoSuchKey";            //e.printStackTrace();           }         catch(NoSuchValueException e)        ...{            value = "NoSuchValue";            //e.printStackTrace();           }        catch (RegistryException e)        ...{                 e.printStackTrace();            }        return value;    }  /**//*    //测试    public static void main(String[] args)    {        String today = shortDateFormat.format(new java.util.Date());        setValue("SOFTWARE","Plan\RegistInfo","RegistDate",today);        String value = getValue("SOFTWARE","Plan\RegistInfo","LastLoginDate");        System.out.println(getValue("SOFTWARE","Plan\RegistInfo","LastLoginDate"));        //deleteValue("SOFTWARE","Plan\RegistInfo","LastLoginDate");                 java.util.Timer timer = new java.util.Timer();             timer.scheduleAtFixedRate(new TimerTask(){             public void run()             {                 System.out.println(getValue("SOFTWARE","EPlan\RegistInfo","LastLoginDate"));             }          },5000,2000);            }  */}

0 0