1

我想使用 Runtime.exec() 更新注册表以HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 使用 WindowsREG命令实用程序。

需要能够从“运行”键添加/删除/读取条目以允许我的 Swing 应用程序在启动时运行并检查它是否配置为在启动时运行,以便我可以在 GUI 中将该选项标记为选中或未选中. 我使用 JNI 进行了此操作,但该库仅支持 32 位,因此不适用于 64 位。我认为这将是一个更好的方法。甚至不需要以这种方式包含一个库,我认为REG它不会消失或改变。

有没有人这样做过或知道如何做到这一点?

谢谢!

4

2 回答 2

0

这种方法可能无法按预期工作。在 x64 Windows 中查看是否有一种方法可以运行 Runtime.exec() 进程以避免“注册表重定向”

于 2010-10-06T02:02:06.657 回答
0

我在此处找到的示例中添加了几个新方法(addValue/deleteValue): read/write to Windows Registry using Java

/**
 * @author Oleg Ryaboy, based on work by Miguel Enriquez
 */
public class WindowsReqistry
{

    /**
     * 
     * @param location
     *          path in the registry
     * @param key
     *          registry key
     * @return registry value or null if not found
     */
    public static final String readRegistry(String location, String key)
    {
        try
        {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec("reg query \"" + location + "\" /v \"" + key + "\"");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            if (!output.contains("\t"))
            {
                return null;
            }

            // Parse out the value
            String[] parsed = output.split("\t");
            if(parsed.length > 0)
            {
                String result = parsed[parsed.length - 1].trim();
                result = result.substring(1, result.length() - 1);
                return result;
            }
        }
        catch (Exception e)
        {
        }
        return null;
    }

    static class StreamReader extends Thread
    {
        private InputStream is;
        private StringWriter sw = new StringWriter();;

        public StreamReader(InputStream is)
        {
            this.is = is;
        }

        public void run()
        {
            try
            {
                int c;
                while ((c = is.read()) != -1)
                    sw.write(c);
            }
            catch (IOException e)
            {
            }
            finally
            {
                try
                {
                    is.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        public String getResult()
        {
            return sw.toString();
        }
    }

    public static boolean deleteValue(String key, String valueName)
    {
        try
        {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec("reg delete \"" + key + "\" /v \"" + valueName + "\" /f");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            return output.contains("The operation completed successfully");
        }
        catch (Exception e)
        {
        }
        return false;
    }

    public static boolean addValue(String key, String valName, String val)
    {
        try
        {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec(
                    "reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            return output.contains("The operation completed successfully");
        }
        catch (Exception e)
        {
        }
        return false;
    }

}
于 2010-10-06T02:04:24.510 回答