下面是一个小小的例子,它可以取得WIndows2000、WindowsXP系统中当前登录用户的用户名和主文件夹,一下代码Copy过去就可以运行。
Good Luck ;)
import java.util.*;
import java.io.*;
public class CurrentUser {
public CurrentUser() {
Process p = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
try
{
p = r.exec( "cmd.exe /c set user" );
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);
System.out.println(key + " = " + value);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
CurrentUser currentUser1 = new CurrentUser();
}
}