Lendo registro especifico do windows

2 respostas
W

Buenas pessoal,

to precisando ler o conteudo deste registro aqui

“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common Startup”

para que em cada windows eu possa saber qual é sua pasta “Inicializar”

e assim jogar meu JNLP la dentro ( para que meu o aplicativo inicialize com a maquina ).

vlw aew.

2 Respostas

Scorsatto

Tente isto:

package teste;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.swing.JOptionPane;

/**
 * @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");
            return parsed[parsed.length-1];
        }
        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) { 
        }
        }

        public String getResult() {
            return sw.toString();
        }
    }
    public static void main(String[] args) {

        // Sample usage
        String value = WindowsReqistry.readRegistry("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" 
                 + "Explorer\\Shell Folders", "Startup");
        JOptionPane.showMessageDialog(null, value);
        System.out.println(value);
    }
}
W

No windows 7 não funciona!

alguem teria otro exemplo?

vlw.

Criado 12 de janeiro de 2011
Ultima resposta 12 de jan. de 2011
Respostas 2
Participantes 2