通过Mac电脑获取java中的硬盘序列号?

时间:2017-11-15 11:45:19

标签: java macos

我在Java中构建一个简单的类,它返回计算机磁盘的序列号。所以这是代码:

package utility;

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestReadSerialNumber {
  private TestReadSerialNumber() {  }

  public static String getSerialNumber(String drive) {
  String result = "";
    try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);

      String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                  +"Set colDrives = objFSO.Drives\n"
                  +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                  +"Wscript.Echo objDrive.SerialNumber";  // see note
      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
         result += line;
      }
      input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
  }

  public static void main(String[] args){
    String sn = TestReadSerialNumber.getSerialNumber("C");
    System.out.println(sn);}
}

现在,如果我尝试从Windows启动此代码,我会得到正确的结果。如果我尝试在Mac计算机上启动此代码,我就不会从getSerialNumber方法中获取字符串。

我认为问题出在系统操作中,我的代码只与Windows兼容,而不与Mac兼容。 那么,如何通过Mac电脑获取序列号?

0 个答案:

没有答案