如何在 GUI 上获取主板序列号(在 java 中)
how to get motherboard serial number on GUI (in java)
我确实喜欢在文本字段(GUI 面板)中显示主板序列号。我创建了一个文本字段和操作按钮。我在操作按钮中编写了这段代码。我在这段代码中犯了什么错误?
try {
Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (true) {
String line = inn.readLine();
if (line == null) {
break;
}
motherboard.setText(line);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Sorry could not found motherboard serial!");
}
你应该使用另一种形式的 Runtime.exec :
String[] command = {"wmic", "baseboard", "get", "serial number"};
Process p = Runtime.getRuntime().exec(command);
问题是您读取了 multi-line
输出
while (true) {
String line = inn.readLine();
if (line == null) {
break;
}
...
但是你总是只存储当前在文本字段中读取的行。意味着之前的输出被覆盖。
...
motherboard.setText(line);
}
由于输出的最后一行是 empty line
您的文本字段显示此空行(意味着您看不到任何输出)。
编辑 以下仅为完整性添加。
一个可以当String serialNumber = getSerialNumber()
使用的小方法。它过滤掉 header 行和空行。
static String getSerialNumber() throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("wmic", "baseboard",
"get", "serialnumber");
Process process = pb.start();
process.waitFor();
String serialNumber = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream()))) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
if (line.length() < 1 || line.startsWith("SerialNumber")) {
continue;
}
serialNumber = line;
break;
}
}
return serialNumber;
}
另一种方法可能是在 wmic
命令上进行过滤并只读取输出的第一行。
或者使用 Windows
提供的命令行工具
wmic baseboard get serialnumber | findstr /r /v "^$" | findstr /v "SerialNumber"
或使用自定义 XSL
来控制 wmic
的输出。
另存为simple.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/"><xsl:apply-templates select="COMMAND/RESULTS"/>
</xsl:template>
</xsl:stylesheet>
和运行命令为
wmic baseboard get serialnumber /Format:.\simple
try
{
String result = null;
Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
BufferedReader input
= new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null)
{
result += line;
}
if (result.equalsIgnoreCase(" ")) {
System.out.println("Result is empty");
} else
{
motherboard.setText(result);
}
input.close();
} catch (IOException ex)
{
Exceptions.printStackTrace(ex);
}
private void serial(){
// wmic command for diskdrive id: wmic DISKDRIVE GET SerialNumber
// wmic command for cpu id : wmic cpu get ProcessorId
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[] { "wmic", "bios", "get", "SerialNumber" });
//process = Runtime.getRuntime().exec(new String[] { "wmic", "DISKDRIVE", "get", "SerialNumber" });
// process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" });
//process = Runtime.getRuntime().exec(new String[] { "wmic", "baseboard", "get", "SerialNumber" });
process.getOutputStream().close();
} catch (IOException ex) {
Logger.getLogger(Activate.class.getName()).log(Level.SEVERE, null, ex);
}
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
String serial = sc.next();
System.out.println(property + ": " + serial);
this.serial.setText(property + ": " + serial);
}
getBufferReader("wmic logicaldisk get volumeserialnumber");
getBufferReader("wmic csproduct get UUID");
getBufferReader("wmic csproduct get name");
getBufferReader("wmic CPU get ProcessorId");
getBufferReader("wmic CPU get Architecture");
getBufferReader("wmic CPU get NumberOfCores");
getBufferReader("wmic CPU get ProcessorType");
getBufferReader("wmic CPU get Revision");
getBufferReader("wmic CPU get Family");
getBufferReader("wmic CPU get Level");
getBufferReader("wmic CPU get Name");
getBufferReader("wmic CPU get SerialNumber");
getBufferReader("wmic bios get Manufacturer");
getBufferReader("wmic bios get Version");
getBufferReader("wmic bios get SMBIOSBIOSVersion");
getBufferReader("wmic bios get SMBIOSMajorVersion");
getBufferReader("wmic bios get SMBIOSMinorVersion");
getBufferReader("wmic bios get ReleaseDate");
getBufferReader("wmic bios get serialnumber");
getBufferReader("wmic baseboard get serialnumber");
getBufferReader("wmic DISKDRIVE get SerialNumber");
getBufferReader("wmic useraccount get name,sid");
public static String getBufferReader(String getCode) throws IOException, Exception {
StringBuilder output = new StringBuilder();
Process processRun = Runtime.getRuntime().exec(getCode);
BufferedReader processBuf = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String line;
while ((line = processBuf.readLine()) != null) {
output.append(line).append("\n");
}
return output.toString().substring(output.indexOf("\n"), output.length()).trim().replace("-", "");
}
我确实喜欢在文本字段(GUI 面板)中显示主板序列号。我创建了一个文本字段和操作按钮。我在操作按钮中编写了这段代码。我在这段代码中犯了什么错误?
try {
Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (true) {
String line = inn.readLine();
if (line == null) {
break;
}
motherboard.setText(line);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Sorry could not found motherboard serial!");
}
你应该使用另一种形式的 Runtime.exec :
String[] command = {"wmic", "baseboard", "get", "serial number"};
Process p = Runtime.getRuntime().exec(command);
问题是您读取了 multi-line
输出
while (true) {
String line = inn.readLine();
if (line == null) {
break;
}
...
但是你总是只存储当前在文本字段中读取的行。意味着之前的输出被覆盖。
...
motherboard.setText(line);
}
由于输出的最后一行是 empty line
您的文本字段显示此空行(意味着您看不到任何输出)。
编辑 以下仅为完整性添加。
一个可以当String serialNumber = getSerialNumber()
使用的小方法。它过滤掉 header 行和空行。
static String getSerialNumber() throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("wmic", "baseboard",
"get", "serialnumber");
Process process = pb.start();
process.waitFor();
String serialNumber = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream()))) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
if (line.length() < 1 || line.startsWith("SerialNumber")) {
continue;
}
serialNumber = line;
break;
}
}
return serialNumber;
}
另一种方法可能是在 wmic
命令上进行过滤并只读取输出的第一行。
或者使用 Windows
提供的命令行工具wmic baseboard get serialnumber | findstr /r /v "^$" | findstr /v "SerialNumber"
或使用自定义 XSL
来控制 wmic
的输出。
另存为simple.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/"><xsl:apply-templates select="COMMAND/RESULTS"/>
</xsl:template>
</xsl:stylesheet>
和运行命令为
wmic baseboard get serialnumber /Format:.\simple
try
{
String result = null;
Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
BufferedReader input
= new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null)
{
result += line;
}
if (result.equalsIgnoreCase(" ")) {
System.out.println("Result is empty");
} else
{
motherboard.setText(result);
}
input.close();
} catch (IOException ex)
{
Exceptions.printStackTrace(ex);
}
private void serial(){
// wmic command for diskdrive id: wmic DISKDRIVE GET SerialNumber
// wmic command for cpu id : wmic cpu get ProcessorId
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[] { "wmic", "bios", "get", "SerialNumber" });
//process = Runtime.getRuntime().exec(new String[] { "wmic", "DISKDRIVE", "get", "SerialNumber" });
// process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" });
//process = Runtime.getRuntime().exec(new String[] { "wmic", "baseboard", "get", "SerialNumber" });
process.getOutputStream().close();
} catch (IOException ex) {
Logger.getLogger(Activate.class.getName()).log(Level.SEVERE, null, ex);
}
Scanner sc = new Scanner(process.getInputStream());
String property = sc.next();
String serial = sc.next();
System.out.println(property + ": " + serial);
this.serial.setText(property + ": " + serial);
}
getBufferReader("wmic logicaldisk get volumeserialnumber");
getBufferReader("wmic csproduct get UUID");
getBufferReader("wmic csproduct get name");
getBufferReader("wmic CPU get ProcessorId");
getBufferReader("wmic CPU get Architecture");
getBufferReader("wmic CPU get NumberOfCores");
getBufferReader("wmic CPU get ProcessorType");
getBufferReader("wmic CPU get Revision");
getBufferReader("wmic CPU get Family");
getBufferReader("wmic CPU get Level");
getBufferReader("wmic CPU get Name");
getBufferReader("wmic CPU get SerialNumber");
getBufferReader("wmic bios get Manufacturer");
getBufferReader("wmic bios get Version");
getBufferReader("wmic bios get SMBIOSBIOSVersion");
getBufferReader("wmic bios get SMBIOSMajorVersion");
getBufferReader("wmic bios get SMBIOSMinorVersion");
getBufferReader("wmic bios get ReleaseDate");
getBufferReader("wmic bios get serialnumber");
getBufferReader("wmic baseboard get serialnumber");
getBufferReader("wmic DISKDRIVE get SerialNumber");
getBufferReader("wmic useraccount get name,sid");
public static String getBufferReader(String getCode) throws IOException, Exception {
StringBuilder output = new StringBuilder();
Process processRun = Runtime.getRuntime().exec(getCode);
BufferedReader processBuf = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String line;
while ((line = processBuf.readLine()) != null) {
output.append(line).append("\n");
}
return output.toString().substring(output.indexOf("\n"), output.length()).trim().replace("-", "");
}