参考:http://blog.csdn.net/brilliancezhou/article/details/5425754
/**
*Java 操作注册表,为文件夹添加右键菜单cmd
*author: zhouh
*/
import java.io.UnsupportedEncodingException;
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
public classRegisterManager{
/*定义目录字符串*/
// private static String dir ="Directory//shell";
private static String dir = "Folder\\shell";
/*注册表键对象*/
private static RegistryKey rkey;
/*为dir添加set和get方法*/
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
/*构造函数,打开HKEY_CLASSES_ROOT中的dir*/
private RegisterManager() {
rkey = Registry.openSubkey(Registry.HKEY_CLASSES_ROOT,
dir , RegistryKey.ACCESS_WRITE);
}
/*创建子目录方法*/
public void createSubDir(String subDirName,String key,String defaultValue) throws RegistryException {
/*根据传入的参数创建子目录*/
rkey.createSubKey(subDirName, "");
/*打开当前创建的子目录,设置为可写状态*/
rkey = Registry.openSubkey(Registry.HKEY_CLASSES_ROOT,dir+"\\"+subDirName,RegistryKey.ACCESS_WRITE);
/*设置当前子目录的默认注册表项的值*/
rkey.setValue(new RegStringValue(rkey, key, defaultValue));
/*重新设置当前目录,为方便此目录下再添加子目录*/
this.setDir(dir+"\\"+subDirName);
}
publicstaticvoidmain(String[] args) throws NoSuchKeyException, RegistryException,
UnsupportedEncodingException{
/*创建对象*/
RegisterManager rm = new RegisterManager();
/*创建子项CommandPrompt,默认的value值为cmd, 那么在文件夹的右键菜单下即会显示cmd*/
rm.createSubDir("CommandPrompt","","Test_DOS");
/*在CommandPrompt项下创建子项Command,值为(cmd.exe /k cd /"%1/") 也就是单击cmd所要执行的操作*/
rm.createSubDir("Command","","cmd.exe /k cd \"%1\" ");
//Explorer.exe /e,/root,%L
}
}
效果: