问题
I'm trying to modify Windows registry so that the context menu for certain audio file extensions so that is an option to start my application on the said files. The application is written Java but wrapped in an exe wrapper using Winrun4j.
Ive been using this example How do I make context menus for my software? to try and do this. The frustrating thing is the demo adds a context menu for .txt files and that works, and if I hack the code and add it to my application for .txt file it also works. But if I then just change the code to work on audio files such as .mp3 no errors are reported but it doesn't work, no option is shown on the context menu for .mp3s
The code check to see if a particular group is associated with another application and if it is that affects the the value entered into registry, but both audio files and text files are associated with another application (Audio Vlc, txt, UltraEdit) so that doesn't seem to be the problem. I did try ignoring these applications and adding direct into Software/.mp3/classes instead of Software/Vlc/classes but it made no difference.
Here is the output when I run:
C:\Program Files (x86)\Jthink\SongKong>songkong.exe
10/02/2014 17.13.10:com.jthink.songkong.Windows.AddContextMenu:buildLaunchCommand:SEVERE: Architecture is:x86
10/02/2014 17.13.10:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.10:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.mp3 /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp3
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp3 /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.mp3)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.mp3\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.mp3\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp3\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp3\Shell\SongKong32\Command /ve
/t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: MP3:AddContextMenu:true
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.ogg /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.ogg
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.ogg /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.ogg)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.ogg\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.ogg\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.ogg\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.ogg\Shell\SongKong32\Command /ve
/t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: OGG:AddContextMenu:true
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.flac /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.flac
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.flac /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.flac)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.flac\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.flac\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.flac\Shell\SongKong32 /ve /t REG
_SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.flac\Shell\SongKong32\Command /v
e /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: FLAC:AddContextMenu:true
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4a /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4a
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4a /ve
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.m4a)
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.m4a\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.m4a\Shell\SongKong32
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4a\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.11:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4a\Shell\SongKong32\Command /ve
/t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4A:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.mp4 /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp4
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.mp4 /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.mp4)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.mp4\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.mp4\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp4\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.mp4\Shell\SongKong32\Command /ve
/t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: MP4:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4p /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4p
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.m4p /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.m4p)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.m4p\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.m4p\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4p\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.m4p\Shell\SongKong32\Command /ve
/t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4P:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.wma /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.wma
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC.wma /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\VLC media file (.wma)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\VLC.wma\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\VLC.wma\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.wma\Shell\SongKong32 /ve /t REG_
SZ /d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\VLC.wma\Shell\SongKong32\Command /ve
/t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: WMA:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.m4b /ve
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\(value not set)
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\.m4b\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\.m4b\Shell\SongKong32
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\.m4b\Shell\SongKong32 /ve /t REG_SZ
/d Fix with SongKong /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\.m4b\Shell\SongKong32\Command /ve /t
REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.12:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: M4B:AddContextMenu:true
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: launchCmd:"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe"
-g -m "%1"
10/02/2014 17.13.12:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\.txt /ve
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\UltraEdit.txt
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\UltraEdit.txt /ve
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCR\Text Document
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:createContextMenu:SEVERE: targetKeyNodeHKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG QUERY HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32 /ve /
t REG_SZ /d Fix with SongKong /f
10/02/2014 17.13.13:com.jthink.songkong.Windows.ContextMenuManager:executeCommand:SEVERE: Cmd:REG ADD HKCU\Software\Classes\UltraEdit.txt\Shell\SongKong32\Comma
nd /ve /t REG_SZ /d \"C:\Program Files (x86)\Jthink\SongKong\lib\SongKong.exe\" -g -m \"%1\" /f
10/02/2014 17.13.13:com.jthink.songkong.Windows.AddContextMenu:addContextMenu:SEVERE: txt:AddContextMenu:true
Here is the relevent code
public class AddContextMenu
{
public static void addContextMenu() {
String launchCmd = buildLaunchCommand();
String INTERNAL_NAME = "SongKong";
final String osArch = System.getProperty("os.arch").toLowerCase();
if(!osArch.contains("64"))
{
INTERNAL_NAME = "SongKong";
}
String DISPLAY_NAME = "Fix with SongKong";
for(AudioFormat audioFormat: AudioFormat.values())
{
if(audioFormat.getFileSuffix().length()>0)
{
boolean success = ContextMenuManager.createContextMenu(
INTERNAL_NAME, DISPLAY_NAME, SongKong.class,
"." + audioFormat.getFileSuffix(), launchCmd);
MainWindow.logger.severe(audioFormat+":AddContextMenu:"+success);
}
}
//For testing
boolean success = ContextMenuManager.createContextMenu(
INTERNAL_NAME, DISPLAY_NAME, SongKong.class,
".txt", launchCmd);
MainWindow.logger.severe("txt"+":AddContextMenu:"+success);
}
static protected String buildLaunchCommand()
{
File userDir = new File(System.getProperty("user.dir"));
final String osArch = System.getProperty("os.arch").toLowerCase();
MainWindow.logger.severe("Architecture is:"+osArch);
if(osArch.contains("64"))
{
return "\""+ userDir.getAbsolutePath() + "\\SongKong64.exe\" -g -m \"%1\"";
}
else
{
return "\""+ userDir.getAbsolutePath() + "\\SongKong.exe\" -g -m \"%1\"";
}
}
}
package com.jthink.songkong.Windows;
import com.jthink.songkong.ui.MainWindow;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final public class ContextMenuManager
{
static public boolean createContextMenu(String internalName,
String displayName, Class mainClass, String targetExtention, String launchCmd)
{
MainWindow.logger.severe("launchCmd:"+launchCmd);
String targetKeyNode = inferTargetKeyNode(targetExtention, internalName);
MainWindow.logger.severe("targetKeyNode"+targetKeyNode);
if (keyNodeExists(targetKeyNode))
{
// TODO: Handle existence of Registry key-node with the same name
MainWindow.logger.severe("targetKeyNodeExists"+targetKeyNode);
return false;
}
String[][] registryCmds = buildRegistryAddCommands(
targetKeyNode, displayName, launchCmd);
boolean operationSuccessful = true;
for (int i = 0; i < registryCmds.length; i++)
{
operationSuccessful &= executeCommand(registryCmds[i]);
}
if (!operationSuccessful)
{
removeContextMenu(internalName, targetExtention); // "Roll back"
}
return operationSuccessful;
}
static public boolean removeContextMenu(String internalName,
String targetExtention)
{
/*
* Implementation note:
* This way we might be deleting another app's context-menu entry if
* it happens to have the same name as ours. It may be a good idea to
* add an custom value to our key-node with a name amd or data highly
* unlikely to have been inserted by another app as well. This way we
* should minimize the risk of accidentally deleting another app's
* entry to next to zero.
*/
String targetKeyNode = inferTargetKeyNode(targetExtention, internalName);
if (!keyNodeExists(targetKeyNode))
{
return true;
}
String[][] registryCmds = buildRegistryDeleteCommands(targetKeyNode);
boolean operationSuccessful = true;
for (int i = 0; i < registryCmds.length; i++)
{
operationSuccessful &= executeCommand(registryCmds[i]);
}
if (!operationSuccessful)
{
// TODO: Do some "rolling forward" (?)
// (Rather difficult, so we better ignore it and hope we didn't
// break anything :D)
}
return operationSuccessful;
}
static protected String[][] buildRegistryAddCommands(String targetKeyNode,
String displayName, String launchCmd)
{
String[][] cmds = new String[2][];
// REG ADD <target_key_node>\Shell\<internal_name> /ve /t REG_SZ /d <display_name> /f
cmds[0] = new String[]{
"REG", "ADD", targetKeyNode, "/ve", "/t", "REG_SZ", "/d",
displayName, "/f"};
// REG ADD <target_key_node>\Shell\<internal_name>\Command /ve /t REG_SZ /d <properly_escaped_launch_command> /f
String escapedLaunchCmd = launchCmd.replaceAll("\"", "\\\\\"");
cmds[1] = new String[]{
"REG", "ADD", targetKeyNode + "\\Command", "/ve", "/t", "REG_SZ",
"/d", escapedLaunchCmd, "/f"};
return cmds;
}
static protected String[][] buildRegistryDeleteCommands(
String targetKeyNode)
{
String[][] cmds = new String[1][];
/* REG ADD <target_key_node>\Shell\<internal_name> /f */
cmds[0] = new String[]{"REG", "DELETE", targetKeyNode, "/f"};
return cmds;
}
static protected boolean executeCommand(String[] cmd)
{
return executeCommand(cmd, null);
}
static protected boolean executeCommand(String[] cmd, List<String> output)
{
StringBuffer sb = new StringBuffer();
for(String c:cmd)
{
sb.append(c+" ");
}
MainWindow.logger.severe("Cmd:"+sb);
try
{
ProcessBuilder pb = new ProcessBuilder(cmd);
Process proc = pb.start();
/*BufferedReader brErr = new BufferedReader(new InputStreamReader(
proc.getErrorStream()));
String lineErr;
while ((lineErr = brErr.readLine()) != null) {
System.err.println(lineErr);
}*/
if (output != null)
{
BufferedReader br = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
output.add(line);
}
}
int exitStatus = proc.waitFor();
return (exitStatus == 0);
}
catch (Exception x)
{
x.printStackTrace();
}
return false;
}
static protected String inferTargetKeyNode(String targetExtension,
String internalName)
{
try
{
//Search
//i.e.
Pattern pattern = Pattern.compile(
"\\s*\\(.+\\)\\s*REG_SZ\\s*(.*)");
String[] cmd = {"REG", "QUERY", "", "/ve"};
String classKeyNode = "HKCR\\" + targetExtension;
boolean findAssociatedClass = true;
while (findAssociatedClass)
{
cmd[2] = classKeyNode;
List<String> output = new ArrayList<>();
executeCommand(cmd, output);
findAssociatedClass = false;
//Look to see whther we should associate with suffix directly or via another application (i.e.mp4 or Vlc files)
for (String line : output)
{
Matcher matcher = pattern.matcher(line);
if (matcher.find()
&& keyNodeExists("HKCR\\" + matcher.group(1)))
{
classKeyNode = "HKCR\\" + matcher.group(1);
findAssociatedClass = true;
break;
}
}
}
//Place in User Space rather than root space (which need admin permissions
classKeyNode = classKeyNode
.replaceFirst("HKCR", "HKCU\\\\Software\\\\Classes");
return classKeyNode + "\\Shell\\" + internalName;
}
catch (Exception x)
{
x.printStackTrace();
}
return "";
}
static protected boolean keyNodeExists(String keyNode)
{
if ((keyNode == null) || (keyNode.trim().length() == 0))
{
throw new RuntimeException(
"Invalid or non-existent Registry-key node !");
}
try
{
String[] cmd = {"REG", "QUERY", keyNode};
return executeCommand(cmd);
}
catch (Exception x)
{
x.printStackTrace();
}
return false;
}
}
来源:https://stackoverflow.com/questions/21684149/why-can-i-add-context-menus-for-some-extensions-but-no-others-using-reg-and-java