📄 winregistryutil.java
字号:
* * @param subKey name of the key (not null) * @param regLevel given regLevel * @return string array containing the sub keys * @exception RegisterFailedException */ private static String[] regGetSubKeys(String subKey, int regLevel) { int hKey = getHKeyByLevel(regLevel); return WinRegistryWrapper.WinRegGetSubKeys(hKey, subKey, MAX_KEY_LENGTH); } /** * Gets the default value for a specified key. * * @param subKey Name of the key (not null) * @param regLevel given regLevel * @return content of the default value * @exception RegisterFailedException */ private static String getDefaultValue(String subKey, int regLevel) { int hKey = getHKeyByLevel(regLevel); return WinRegistryWrapper.WinRegQueryValueEx(hKey, subKey, VN_DEFAULT); } /** * Sets the default value for a specified key. * * @param subKey Name of the key (not null) * @param value Value to be set (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ private static void setDefaultValue(String subKey, String value, int regLevel) throws RegisterFailedException { // if the specified subKey does not exist, create it first if (!isSubKeyExist(subKey, regLevel)) { regCreateKeyEx(subKey, regLevel); } int hKey = getHKeyByLevel(regLevel); if (WinRegistryWrapper.WinRegSetValueEx(hKey, subKey, VN_DEFAULT, value) != ERROR_SUCCESS) { throw new RegisterFailedException("Set default value" + " for key " + subKey + " error."); } WinRegistryWrapper.WinRegFlushKey(hKey, subKey); } /** * Writes an action into the registry table * (From the specified registry folder). * * @param action given action to be added (not null) * @param clsID given class ID (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails */ private static void addActionByClsID(Action action, String clsID, int regLevel) throws RegisterFailedException { String verb = action.getVerb(); String desc = action.getDescription(); String cmd = action.getCommand(); String clsIDKey = getClsIDKey(clsID, regLevel); String shellKey = clsIDKey + "\\" + KN_SHELL; String verbKey = shellKey + "\\" + verb; String cmdKey = verbKey + "\\" + KN_COMMAND; if (cmdKey != null) { regCreateKeyEx(cmdKey, regLevel); if (cmd != null) { setDefaultValue(cmdKey, cmd, regLevel); if ((desc != null) && (verbKey != null)) { setDefaultValue(verbKey, desc, regLevel); } } } } /** * Returns the mime type associated with the given file extension * (From the given registry folder). * * @param fileExt given file extension (not null) * @param regLevel given regLevel * @return corresponding mime type, or null if not exists */ public static String getMimeTypeByFileExt(String fileExt, int regLevel) { String fileExtKey = getFileExtKey(fileExt, regLevel); if (fileExtKey != null) { return regQueryValueEx(fileExtKey, VN_CONTENT, regLevel); } else { return null; } } /** * Returns the mime type information associated with the given file extension * (From HKEY_ROOT registry level). * * @param fileExt given file extension (not null) * @return corresponding mime type, or null if not exists */ public static String getMimeTypeByFileExt(String fileExt) { return (getMimeTypeByFileExt(fileExt, ROOT_LEVEL)); } /** * Sets the mime type associated with the given file extension. * * @param mimeType given mime type (not null) * @param fileExt given file extension (not null) * @param regLevel given reglevel * @throws RegisterFailedException if the given operation fails. */ public static void setMimeTypeByFileExt(String mimeType, String fileExt, int regLevel) throws RegisterFailedException { String fileExtKey = getFileExtKey(fileExt, regLevel); if (fileExtKey != null) { // set the content value regSetValueEx(fileExtKey, VN_CONTENT, mimeType, regLevel); } } /** * Returns the file extensione associated with the given mime type * (From the given registry folder). * * @param mimeType given mime type (not null) * @param regLevel given reglevel * @return corresponding file extension, or null if none */ public static String getFileExtByMimeType(String mimeType, int regLevel) { String mimeSubKey = getMimeTypeKey(mimeType, regLevel); if (mimeSubKey != null) { return regQueryValueEx(mimeSubKey, VN_EXTENSION, regLevel); } else { return null; } } /** * Returns the file extension associated with the given mime type * (From HKEY_ROOT registry folder). * * @param mimeType given mime type (not null) * @return corresponding file extension, or null if none */ public static String getFileExtByMimeType(String mimeType) { return (getFileExtByMimeType(mimeType, ROOT_LEVEL)); } /** * Sets the file extensione associated with the given mime type. * * @param fileExt given file extension (not null) * @param mimeType given mime type (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void setFileExtByMimeType(String fileExt, String mimeType, int regLevel) throws RegisterFailedException { String mimeSubKey = getMimeTypeKey(mimeType, regLevel); if (mimeSubKey != null) { regSetValueEx(mimeSubKey, VN_EXTENSION, fileExt, regLevel); } } /** * Returns the icon file name associated with the given file extension. * * @param fileExt given file extension (not null) * @param regLevel given regLevel * @return corresponding icon file, or null if none */ public static String getIconFileNameByFileExt(String fileExt, int regLevel) { // Retrieve the icon key String iconKey = getIconKey(fileExt, regLevel); if (iconKey == null) { return null; } String unDealedFileName = getDefaultValue(iconKey, regLevel); if (unDealedFileName == null) { return null; } return ExpandEnvironmentStrings(unDealedFileName); } /** * Retrievs the icon file of the specified file extension (From HKEY_ROOT * folder). * * @param fileExt * given file extension (not null) * @return corresponding icon file, or null if none */ public static String getIconFileNameByFileExt(String fileExt) { return (getIconFileNameByFileExt(fileExt, ROOT_LEVEL)); } /** * Sets the icon file name associated with the given file extension. * * @param iconFileName given icon file name (not null) * @param fileExt given file extension name (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void setIconFileNameByFileExt(String iconFileName, String fileExt, int regLevel) throws RegisterFailedException { // Get the icon key String iconKey = getIconKey(fileExt, regLevel); if (iconKey == null) { // If the classID do not created yet, create it first String temClassID = genClassID(fileExt, regLevel); if (temClassID != null) { setClassIDByFileExt(fileExt, temClassID, regLevel); iconKey = getIconKey(fileExt, regLevel); } } if (iconKey != null) { // Set the default value of the iconkye as the iconfile setDefaultValue(iconKey, iconFileName, regLevel); } } /** * Returns the description associated with the given file extension. * * @param fileExt given file extension (not null) * @param regLevel given regLevel * @return corresponding description about the file extension, or null if none */ public static String getDescriptionByFileExt(String fileExt, int regLevel) { // Retrievs the class ID String classID = getClassIDByFileExt(fileExt, regLevel); if (classID != null) { String clsIDKey = getClsIDKey(classID, regLevel); if (clsIDKey != null) { // The default value of the class ID key is the description return getDefaultValue(clsIDKey, regLevel); } } return null; } /** * Returns the description of the given file extension * (From HKEY_ROOT registry folder). * * @param fileExt given file extension (not null) * @return corresponding description about the file extension, or null if none */ public static String getDescriptionByFileExt(String fileExt) { return (getDescriptionByFileExt(fileExt, ROOT_LEVEL)); } /** * Sets the Description associated with the given file extension. * * @param description given description (not null) * @param fileExt given file extension name (not null) * @param regLevel given registeration level * @throws RegisterFailedException if the operation fails. */ public static void setDescriptionByFileExt(String description, String fileExt, int regLevel) throws RegisterFailedException { String classID = getClassIDByFileExt(fileExt, regLevel); if (classID == null) { // If the classID does not exist, create it first classID = genClassID(fileExt, regLevel); if (classID != null) { setClassIDByFileExt(fileExt, classID, regLevel); } } if (classID != null) { String clsIDKey = getClsIDKey(classID, regLevel); if (clsIDKey != null) { // Default value of the class ID key will be the description setDefaultValue(clsIDKey, description, regLevel); } } } /** * Marks the generator value field of the classID key of the given file extension. * * @param fileExt given file extension name (not null) * @param regLevel given registeration level * @throws RegisterFailedException if the operation fails. */ public static void markGeneratorByFileExt(String fileExt, int regLevel) throws RegisterFailedException { //Get the corresponding class ID key String clsID = getClassIDByFileExt(fileExt, regLevel); String clsIDKey = getClsIDKey(clsID, regLevel); if (clsIDKey != null) { regSetValueEx(clsIDKey, VN_DEFAULTGENERATOR, VALUE_DEFAULTGENERATOR, regLevel); } } /** * Returns the action list associated with the given file extension * (From specified registiry folder). * * @param fileExt given file extension (not null) * @return the action list */ public static List getActionListByFileExt(String fileExt, int regLevel) { List actionList = null; // Retrievs the relevant class ID String clsID = getClassIDByFileExt(fileExt, regLevel); if (clsID!= null) { String clsIDKey = getClsIDKey(clsID, regLevel); String shellKey = clsIDKey + "\\" + KN_SHELL; String verbs[] = null; if (shellKey != null) { verbs = regGetSubKeys(shellKey, regLevel); } if (verbs != null) { int verbsNum = verbs.length; // Construct relevant actions one by one if (verbsNum > 0) { actionList = new ArrayList(); for (int i = 0; i < verbsNum; i++) { String verbKey = shellKey + "\\" + verbs[i]; String cmdKey = verbKey + "\\" + KN_COMMAND; if (cmdKey != null) { Action oneAction; String temCmd = getDefaultValue(cmdKey, regLevel); //In case cmd is a null string, we shall replace it with a empty string if (temCmd == null) { temCmd = ""; } else { temCmd = ExpandEnvironmentStrings(temCmd); } oneAction = new Action(verbs[i], temCmd, getDefaultValue(verbKey, regLevel)); actionList.add(oneAction); } } } } } return actionList; } /** * Returns the action list associated with the specified file extension * (From HKEY_ROOT). * * @param fileExt given file extension (not null) * @return the action list */ public static List getActionListByFileExt(String fileExt) { List rootActionList = getActionListByFileExt(fileExt, ROOT_LEVEL); List userDefinedList = getUserAddedActionListByFileExt(fileExt); if (userDefinedList != null) { return userDefinedList; } else { return rootActionList; } } /** * Returns the action list associated with the user defined file extension * <p> * <B>Note:</B> Windows 2000 will save user added file extension under a * special place. * For example, user add a new file extension .aoo and specify notepad.exe
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -