📄 registrykey.java
字号:
/*
** Java native interface to the Windows Registry API.
**
** Authored by Timothy Gerard Endres
** <mailto:time@gjt.org> <http://www.trustice.com>
**
** This work has been placed into the public domain.
** You may use this work in any way and for any purpose you wish.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package com.ice.jni.registry;
import java.io.PrintWriter;
import java.util.*;
/**
* The RegistryKey class represents a key in the registry.
* The class also provides all of the native interface calls.
*
* You should refer to the Windows Registry API documentation
* for the details of any of the native methods. The native
* implementation performs almost no processing before or after
* a given call, so their behavior should match the API's
* documented behavior precisely.
*
* Note that you can not open a subkey without an existing
* open RegistryKey. Thus, you need to start with one of the
* top level keys defined in the Registry class and open
* relative to that.
*
* @version 3.1.3
*
* @see com.ice.jni.registry.Registry
* @see com.ice.jni.registry.RegistryValue
*/
public class
RegistryKey
{
/**
* Constants used to determine the access level for
* newly opened keys.
*/
public static final int ACCESS_DEFAULT = 0;
public static final int ACCESS_READ = 1;
public static final int ACCESS_WRITE = 2;
public static final int ACCESS_EXECUTE = 3;
public static final int ACCESS_ALL = 4;
/**
* This is the actual DWORD key that is returned from the
* Registry API. This value is <strong>totally opaque</strong>
* and should never be referenced.
*/
protected int hKey;
/**
* The full pathname of this key.
*/
protected String name;
/**
* Used to indicate whether or not the key was created
* when method createSubKey() is called, otherwise false.
*/
protected boolean created;
public
RegistryKey( int hKey, String name )
{
this.hKey = hKey;
this.name = name;
this.created = false;
}
public
RegistryKey( int hKey, String name, boolean created )
{
this.hKey = hKey;
this.name = name;
this.created = created;
}
/**
* The finalize() override checks to be sure the key is closed.
*/
public void
finalize()
{
// Never close a top level key...
if ( this.name.indexOf( "\\" ) > 0 )
{
// REVIEW should we have an "open/closed" flag
// to avoid double closes? Or is it better to
// lazily not call closeKey() and let finalize()
// do it all the time?
//
try { this.closeKey(); }
catch ( RegistryException ex )
{ }
}
}
/**
* Get the name of this key. This is <em>not</em> fully
* qualified, which means that the name will not contain
* any backslashes.
*
* @return The relative name of this key.
*/
public String
getName()
{
int index = this.name.lastIndexOf( "\\" );
if ( index < 0 )
return this.name;
else
return this.name.substring( index + 1 );
}
/**
* Get the full name of the key, from the top level down.
*
* @return The full name of the key.
*/
public String
getFullName()
{
return this.name;
}
/**
* Determine if this key was opened or created and opened.
* The result can only be true if createSubKey() was called
* and the key did not exist, and the creation of the new
* subkey succeeded.
*
* @return True if the key was created new, else false.
*/
public boolean
wasCreated()
{
return this.created;
}
/**
* Used to set the <em>created</em> state of this key.
*
* @param created The new <em>created</em> state.
*/
public void
setCreated( boolean created )
{
this.created = created;
}
/**
* Open a Registry subkey of this key with READ access.
*
* @param subkey The name of the subkey to open.
* @return The newly opened RegistryKey.
*
* @exception NoSuchKeyException If the subkey does not exist.
* @exception RegistryException Any other registry API error.
*/
public RegistryKey
openSubKey( String subkey )
throws NoSuchKeyException, RegistryException
{
return this.openSubKey( subkey, ACCESS_READ );
}
/**
* Create, and open, a Registry subkey of this key with WRITE access.
* If the key already exists, it is opened, otherwise it is first
* created and then opened.
*
* @param subkey The name of the subkey to create.
* @param className The className of the created subkey.
* @return The newly created and opened RegistryKey.
*
* @exception RegistryException Any valid registry API error.
*/
public RegistryKey
createSubKey( String subkey, String className )
throws RegistryException
{
return this.createSubKey( subkey, "", ACCESS_WRITE );
}
/**
* Set the value of this RegistryKey.
*
* @param value The value to set, including the value name.
*
* @exception RegistryException Any valid registry API error.
*/
public void
setValue( RegistryValue value )
throws RegistryException
{
this.setValue( value.getName(), value );
}
//
// N A T I V E M E T H O D S
//
/**
* Open a Registry subkey of this key with the specified access.
*
* @param subkey The name of the subkey to open.
* @param access The access level for the open.
* @return The newly opened RegistryKey.
*
* @exception NoSuchKeyException If the subkey does not exist.
* @exception RegistryException Any other registry API error.
*/
public native RegistryKey
openSubKey( String subKey, int access )
throws NoSuchKeyException, RegistryException;
/**
* Connect to the remote registry on <em>hostName</em>.
* This method will only work when invoked on a toplevel
* key. The returned value will be the same toplevel key
* opened from the remote host's registry.
*
* @param hostName The remote computer's hostname.
* @return The remote top level key identical to this top level key.
*
* @exception NoSuchKeyException If the subkey does not exist.
* @exception RegistryException Any other registry API error.
*/
public native RegistryKey
connectRegistry( String hostName )
throws NoSuchKeyException, RegistryException;
/**
* Create a new subkey, or open the existing one. You can
* determine if the subkey was created, or whether an
* existing subkey was opened, via the wasCreated() method.
*
* @param subKey The name of the subkey to create/open.
* @param className The key's class name, or null.
* @param access The access level of the opened subkey.
* @return The newly created or opened subkey.
*
* @exception RegistryException Any valid registry API error.
*/
public native RegistryKey
createSubKey( String subKey, String className, int access )
throws RegistryException;
/**
* Closes this subkey. You may chose to let the finalize()
* method do the close.
*
* @exception RegistryException Any valid registry API error.
*/
public native void
closeKey()
throws RegistryException;
/**
* Delete a named subkey.
*
* @param subKey The name of the subkey to delete.
*
* @exception NoSuchKeyException If the subkey does not exist.
* @exception RegistryException Any other registry API error.
*/
public native void
deleteSubKey( String subKey )
throws NoSuchKeyException, RegistryException;
/**
* Delete a named value.
*
* @param valueName The name of the value to delete.
*
* @exception NoSuchValueException If the value does not exist.
* @exception RegistryException Any other registry API error.
*/
public native void
deleteValue( String valueName )
throws NoSuchValueException, RegistryException;
/**
* Guarentees that this key is written to disk. This
* method should be called only when needed, as it has
* a huge performance cost.
*
* @exception RegistryException Any valid registry API error.
*/
public native void
flushKey()
throws RegistryException;
/**
* Set the name value to the given data.
*
* @param valueName The name of the value to set.
* @param value The data to set the named value.
*
* @exception RegistryException Any valid registry API error.
*/
public native void
setValue( String valueName, RegistryValue value )
throws RegistryException;
/**
* Get the data of a named value.
*
* @param valueName The name of the value to get.
* @return The data of the named value.
*
* @exception NoSuchValueException If the value does not exist.
* @exception RegistryException Any other registry API error.
*/
public native RegistryValue
getValue( String valueName )
throws NoSuchValueException, RegistryException;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -