Search code examples
javaoperating-systemregistry

Get OS Information Correctly in Java or C#


I want to get OS name with edition using java or C# as mentioned in below example :

Example : Microsoft Windows 8.1 Enterprise

I have tried below :

  1. In java tried System.getProperty("os.name") => It does not provide OS edition

  2. One ways is to get OS name from registry Getting "ProductName" from \HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion

  3. I also tried SYSTEMINFO command, it provides OS Name. but since it shows output different language. its not possible to check variable name from output.

But I want to find a way other than finding from registry, so that even if in future versions of OS, registry path changes, App should provide correct OS name.

Is there any other reliable way to find OS name with edition ?


Solution

  • Finally I used below code because I analysed that most probably registry path will not be changed.

        private final String STR_OS_NAME_REGISTRY_QUERY = "reg query \"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\" /v \"ProductName\"";     
    
        // Get OS name
            objProcessName = Runtime.getRuntime().exec(STR_OS_NAME_REGISTRY_QUERY); 
            objProcessName.waitFor(); 
            objBufferReader = new BufferedReader(new InputStreamReader(objProcessName.getInputStream())); 
            lstJavaInfo = new ArrayList<>();
    
            while ((sLine = objBufferReader.readLine()) != null)
            {
                lstJavaInfo.add(sLine);
            }
            objProcessName.waitFor();
    
            if(lstJavaInfo.size() < 3)
            {
                return "-";
            }
    
            String[] sarr = lstJavaInfo.get(2).split("\\s+");
            for(int nIndex = 3 ; nIndex < sarr.length ; nIndex++)
            {
                sOSArchitecture = sOSArchitecture + sarr[nIndex] + " ";
            }