簡體   English   中英

如何使用 Java 從 Windows 注冊表讀取 integer 值?

[英]How to read integer value from Windows registry using Java?

我找到了關於這個問題的第一個回復: Read/write to Windows registry using Java

我從回復中復制了class並使用了它。

此示例代碼有效:

String value = WinRegistry.readString (
    WinRegistry.HKEY_LOCAL_MACHINE,                             //HKEY
   "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",           //Key
   "ProductName");                                              //ValueName
    System.out.println("Windows Distribution = " + value);

但是,當我嘗試運行時:

String value = WinRegistry.readString(
                WinRegistry.HKEY_LOCAL_MACHINE,                             //HKEY
                "SYSTEM\\CurrentControlSet\\Services\\LanmanWorkstation\\Parameters",           //Key
                "EnableSecuritySignature");                                              //ValueName
        System.out.println(value);

我得到 null。

從我必須閱讀的組件的注冊表編輯器屏幕

我必須讀取 EnableSecuritySignature 的日期值。

有人知道可能是什么問題嗎? 抱歉,我的描述中缺少細節,我很着急))

我找到了解決方案:

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

public class ReadRegistry
{
    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();

            // Parse out the value
            // String[] parsed = reader.getResult().split("\\s+");

            String s1[];
            try{
             s1=reader.getResult().split("REG_SZ|REG_DWORD");
            }
            catch(Exception e)
            {
                return " ";
            }
           //MK System.out.println(s1[1].trim());

            return s1[1].trim();
        } 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)
                    //System.out.println(c);
                    sw.write(c);
            } catch (IOException e) {
            }
        }

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

你必須像這樣使用它:

Registry2.readRegistry("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\LanmanWorkstation\\Parameters", "EnableSecuritySignature");

class 取自此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM