No Perfect But Best >>
<< Re: 这个字谁认识呀
Read Environment Variable in Windows

Author Zhou Renjian Create@ 2005-04-25 16:50
whizz Note icon
    /**
     *
     */
    private static String getSystemFontsPath() {
        if (systemFontsPath == null) {
            String os = System.getProperty("os.name"); //$NON-NLS-1$
            if (os != null && os.indexOf("Windows") != -1) { //$NON-NLS-1$
                Properties envs = getWindowsEnvironments(os);
                String sysRoot = envs.getProperty("SystemRoot"); //$NON-NLS-1$
                if (sysRoot != null && sysRoot.length() != 0) {
                    systemFontsPath = new File(sysRoot, "Fonts").getAbsolutePath(); //$NON-NLS-1$
                } else {
                    systemFontsPath = "C:\\WINDOWS\\Fonts"; //$NON-NLS-1$
                }
            } else {
                /*
                 * Linux ?
                 */
                systemFontsPath = "/usr/x11/fonts"; //$NON-NLS-1$
            }
        }
        return systemFontsPath;
    }

    /**
     * @param os
     * @return
     */
    private static Properties getWindowsEnvironments(String os) {
        Properties envVars = new Properties();
        Runtime r = Runtime.getRuntime();
        Process p = null;
        try {
            if (os.indexOf("windows 9") > -1) { //$NON-NLS-1$
                p = r.exec("command.com /c set"); //$NON-NLS-1$
            } else {
                p = r.exec("cmd.exe /c set"); //$NON-NLS-1$
            }
            if (p != null) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    int idx = line.indexOf('=');
                    String key = line.substring(0, idx);
                    String value = line.substring(idx + 1);
                    envVars.setProperty(key, value);
                }
            }
        } catch (IOException e) {
            //
        }
        return envVars;
    }

Reference:
http://www.rgagnon.com/javadetails/java-0150.html
本记录所在类别:
本记录相关记录: