📄 winregistryutil.java
字号:
* as the default application for this file type. * The file extension information will be stored at * HKEY_CURRENT_USER\software\Microsoft\Windows\CurrentVersion\ * Explorer\FileExts\.aoo * with data as * Application REG_SZ notepad.exe * the notepad.exe will be stored at * HKEY_CLASSES_ROOT\Applications\notepad.exe * <p> * * @param fileExt given file extension(no null) * @return the action list */ private static List getUserAddedActionListByFileExt(String fileExt) { String fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt; String valueName = USER_FILE_EXT_VALUENAME; int hKey = WinRegistryWrapper.HKEY_CURRENT_USER; //Get application name, e.g. notepad.exe String appName = WinRegistryWrapper.WinRegQueryValueEx(hKey, fileExtKey, valueName); //Get the corresponding application's shell keys String verbs[] = null; String appShellKey = USER_FILE_EXT_APP_PREFIX + "\\" + appName + "\\" + KN_SHELL; hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT; verbs = WinRegistryWrapper.WinRegGetSubKeys(hKey, appShellKey, 255); //Construct relevant actions one by one List actionList = null; if (verbs != null) { int verbsNum = verbs.length; if (verbsNum > 0) { actionList = new ArrayList(); for (int i = 0; i < verbsNum; i++) { String verbKey = appShellKey + "\\" + verbs[i]; String cmdKey = verbKey + "\\" + KN_COMMAND; if (cmdKey != null) { Action oneAction; String temCmd = getDefaultValue(cmdKey, ROOT_LEVEL); //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, ROOT_LEVEL)); actionList.add(oneAction); } } } } return actionList; } /** * Sets the action list associated with the given file extension. * * @param actionList given action list (not null) * @param fileExt given file extension (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void setActionListByFileExt(List actionList, String fileExt, int regLevel) throws RegisterFailedException { // Retrieves the corresponding class ID String clsID = getClassIDByFileExt(fileExt, regLevel); if (clsID == null) { // If the classID does not exist, create it first clsID = genClassID(fileExt, regLevel); if (clsID != null) { // Bundle the file extension and class ID setClassIDByFileExt(fileExt, clsID, regLevel); } } Action oneAction; if (clsID != null) { if (actionList != null) { Iterator actionIter = actionList.iterator(); // Add action to under the class ID key one by one while (actionIter.hasNext()) { oneAction = (Action) actionIter.next(); if ((oneAction != null) && (clsID != null)) { addActionByClsID(oneAction, clsID, regLevel); } } } } } /** * Returns the mime type associated with the given URL * by checking the content of the URL. * * @param url given URL (not null) * @return corresponding mime type */ public static String getMimeTypeByURL(URL url) { return WinRegistryWrapper.WinFindMimeFromData(url); } /** * Expands environment-variable strings and replaces them with their defined values * E.g: "%SystemRoot%\\system32\\NOTEPAD.EXE %1" -> "C:\\system32\\NOTEPAD.EXE %1". * * @param cmdString given command string (not null) * @return String */ public static String ExpandEnvironmentStrings(String cmdString) { return WinRegistryWrapper.WinExpandEnvironmentStrings(cmdString); } /** * Returns true if the specified mime type exists in the Windows registry table. * * @param mimeType given mime type (not null) * @param regLevel given regLevel * @return true if the mime type exists in the registry table */ public static boolean isMimeTypeExist(String mimeType, int regLevel) { // Retrieve the relevant mime type key String mimeTypeKey = getMimeTypeKey(mimeType, regLevel); if (mimeTypeKey != null) { // Checks if the mime type key exists return isSubKeyExist(mimeTypeKey, regLevel); } else { return false; } } /** * Returns true if the mime type exists in the default win registry folder. * * @param mimeType given mimeType (not null) * @return true if the mime type exists in the registry table */ public static boolean isMimeTypeExist(String mimeType) { return (isMimeTypeExist(mimeType, ROOT_LEVEL)); } /** * Returns true if the specified file extension exists in the given registry folder. * * @param fileExt given file extension (not null) * @param regLevel given regLevel * @return true if the file extension exists in the registry table */ public static boolean isFileExtExist(String fileExt, int regLevel) { // Retrieve the relevant file extension key String fileExtKey = getFileExtKey(fileExt, regLevel); if (fileExtKey != null) { // check if this key exists //For windows 2000, we still need to check if the file ext in //Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts return (isSubKeyExist(fileExtKey, regLevel) || isWin2kUserDefinedFileExtExist(fileExt)); } else { return false; } } /** * Returns true if the specified file extension exists in the * \\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts in Windows 2000. * * @param fileExt The given file extension * @return true if the file extension exists. */ public static boolean isWin2kUserDefinedFileExtExist(String fileExt) { boolean win2kFileDefinedByUser = false; if (osName.equalsIgnoreCase(WIN2KOS)) { String fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt; win2kFileDefinedByUser = isSubKeyExist(fileExtKey, USER_LEVEL); } return win2kFileDefinedByUser; } /** * Checks if the speicifed file extension exists in the default registry folder. * * @param fileExt given file extension (not null) * @return true if the file extension exists in the registry table */ public static boolean isFileExtExist(String fileExt) { return (isFileExtExist(fileExt, ROOT_LEVEL)); } /** * Adds a new file extension entry in the Windows registry table under the specified folder. * * @param fileExt name of the new file extension (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void addFileExt(String fileExt, int regLevel) throws RegisterFailedException { // Retrieve the relevant file extension key String fileExtKey = getFileExtKey(fileExt, regLevel); if (fileExtKey != null) { // Create this key in the windows registry table regCreateKeyEx(fileExtKey, regLevel); } } /** * Removes the specified file extension entry from the given folder in the Windows registry table. * * @param fileExt given file extension (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void removeFileExt(String fileExt, int regLevel) throws RegisterFailedException { if (isFileExtExist(fileExt, regLevel)) { // Retrieve the relevant file extension key in Windows regtable String fileExtKey = getFileExtKey(fileExt, regLevel); //Check if we should remove the classID key String clsID = getClassIDByFileExt(fileExt, regLevel); if (clsID != null) { String clsIDKey = getClsIDKey(clsID, regLevel); if (clsIDKey != null) { String value_generator = regQueryValueEx(clsIDKey, VN_DEFAULTGENERATOR, regLevel); if (value_generator.compareTo(VALUE_DEFAULTGENERATOR) == 0) { //The classID is generated by us, we should remove the clssID regDeleteKey(clsIDKey, regLevel); } } } if (fileExtKey != null) { // Delete the key from registry table if (isSubKeyExist(fileExtKey, regLevel)) { regDeleteKey(fileExtKey, regLevel); } } //For windows 2000, we still need to check if the file ext in //Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts if (isWin2kUserDefinedFileExtExist(fileExt)) { fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt; regDeleteKey(fileExtKey, USER_LEVEL); } } } /** * Adds a new MIME type entrty in the Windows registry table under the given folder. * * @param mimeType given mime type (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void addMimeType(String mimeType, int regLevel) throws RegisterFailedException { // Retrieve the relevant mime type key String temMimeKey = getMimeTypeKey(mimeType, regLevel); if (temMimeKey != null) { // Create this mime key regCreateKeyEx(temMimeKey, regLevel); } } /** * Removes the specified mime type entry in the Windows registy table from the given folder. * * @param mimeType given mime type (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void removeMimeType(String mimeType, int regLevel) throws RegisterFailedException { if (isMimeTypeExist(mimeType, regLevel)) { // Retrieve the relvant mime type key String mimeKey = getMimeTypeKey(mimeType, regLevel); if (mimeKey != null) { // Delete the key from the registry table regDeleteKey(mimeKey, regLevel); } } } /** * Sets the class ID value of the specified file extension under the given folder. * * @param fileExt given file extension (not null) * @param classID given class ID value (not null) * @param regLevel given regLevel * @throws RegisterFailedException if the operation fails. */ public static void setClassIDByFileExt(String fileExt, String classID, int regLevel) throws RegisterFailedException { String fileExtKey = getFileExtKey(fileExt, regLevel); String clsIDKey = getClsIDKey(classID, regLevel); if (fileExtKey != null) { /* If the file extension does not exist in the registry table * Add it first */ if (!isSubKeyExist(fileExtKey, regLevel)) { addFileExt(fileExt, regLevel); } /* If the classID does not exist in the registry table * Add it first */ if (!isSubKeyExist(clsIDKey, regLevel)) { if (clsIDKey != null) { regCreateKeyEx(clsIDKey, regLevel); } } setDefaultValue(fileExtKey, classID, regLevel); } } /** * Gets the class ID value of the specified fileExt. * * @param fileExt given file extension (not null) * @param regLevel given regLevel * @return class ID of the this file extension * @exception RegisterFailedException */ public static String getClassIDByFileExt(String fileExt, int regLevel) { //Retrieves the relevant file extension key String fileExtKey = getFileExtKey(fileExt, regLevel); if (fileExtKey != null) { if (isSubKeyExist(fileExtKey, regLevel)) { //in case of having CurVer key return getCurVerClassID(getDefaultValue(fileExtKey,regLevel),regLevel); } } return null; } /** * Gets the real classID in case of having CurVer key * @param defaultVerClassID default version classID * @param regLevel given regLevel * @return CurVerClassID or given classID */ private static String getCurVerClassID(String defaultVerClassID, int regLevel) { String curVerClassIDKey = defaultVerClassID + "\\" + KN_CURVER; if (regLevel != ROOT_LEVEL) { // software\\classes\\*** curVerClassIDKey = SYS_USER_KN_PREFIX + "\\" + curVerClassIDKey; } if (isSubKeyExist(curVerClassIDKey, regLevel)) { //key CurVer exists return getDefaultValue(curVerClassIDKey, regLevel); } else { //key CurVer doesn't exist return defaultVerClassID; } } /** * Sets the mutual reference of the mimeType and fileExt. * * @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 setMutualRef(String fileExt, String mimeType, int regLevel) throws RegisterFailedException { String mimeKey = getMimeTypeKey(mimeType, regLevel); String fileExtKey = getFileExtKey(fileExt, regLevel); if ((mimeKey != null) && (fileExtKey != null)) { if ((isSubKeyExist(fileExtKey, regLevel)) && (isSubKeyExist(mimeKey, regLevel))) { setMimeTypeByFileExt(mimeType, fileExt, regLevel); setFileExtByMimeType(fileExt, mimeType, regLevel); } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -