`
seawavecau
  • 浏览: 755021 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java 读取硬盘Serial Number[转]

 
阅读更多

Get the hard disk serial number or Motherboard serial numberTag(s): Varia
When you need to know hardware details, Java is not the best tool unless you call a JNI routine or an external utility. The JNI solution is always the best because it is designed to interact closely with Java but it may be more complex to develop. If your need is simple (no interaction) and the need to be cross plateform is not present then calling an external utility is maybe "a good enough" choice.

In these 2 examples, we create the appropriate VBS script file on-the-fly and capture its output. They are very Windows oriented since they rely on the "Windows Script Host" to execute the generated scripts.

The vbscript queries a WMI class to get a specific hardware information. Here we are using the Win32_BaseBoard but they are many others, see http://msdn2.microsoft.com/en-us/library/aa389273.aspx for complete list.

Motherboard serial number

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

public class MiscUtils {
  private MiscUtils() {  }

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

      String vbs =
         "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
        + "Set colItems = objWMIService.ExecQuery _ \n"
        + "   (\"Select * from Win32_BaseBoard\") \n"
        + "For Each objItem in colItems \n"
        + "    Wscript.Echo objItem.SerialNumber \n"
        + "    exit for  ' do the first cpu only! \n"
        + "Next \n";

      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 cpuId = MiscUtils.getMotherboardSN();
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null, cpuId, "Motherboard serial number",
         javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}

Hard disk serial number

This serial number is created by the OS where formatting the drive and it's not the manufacturer serial number. It's unique, because it is created on the fly based on the current time information. AFAIK, there is no API that return that the manufacturer SN. At best, the SN of the HD firmware can be read but this will involve some very low-level API calls. Keep in mind that even if you get that number, there is no warranty that it will be unique since each manufacturer can assign the SN as they wish.

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

public class DiskUtils {
  private DiskUtils() {  }

  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 = DiskUtils.getSerialNumber("C");
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null, sn, "Serial Number of C:",
         javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}

NOTE : Other properties : objDrive.AvailableSpace/DriveType/FileSystem/IsReady <!-- HOWTO content -->

分享到:
评论

相关推荐

    用JAVA读取硬盘序列号

    在Java编程环境中,读取硬盘序列号是一项常见的需求,特别是在软件授权、系统识别或设备管理等领域。硬盘序列号是每个硬盘制造商为生产出的每个硬盘分配的唯一标识符,类似于设备的身份证。以下将详细解释如何使用...

    java读取硬盘序列号例子

    在Java编程环境中,读取硬盘序列号是一项常见的需求,特别是在系统管理、设备识别或软件授权等领域。硬盘序列号是每个硬盘制造商赋予的唯一标识符,它可以帮助我们区分不同的硬盘。以下是一个详细的Java实现方法,...

    Get the hard disk serial number or Motherboard serial number in java

    Wscript.Echo objItem.SerialNumber exit for ' do the first CPU only! Next ``` 2. 使用Java的`File`类创建并写入这个VBScript文件。 3. 运行VBScript文件,通过`Runtime.getRuntime().exec()`方法调用`cscript`...

    用JAVA读取硬盘序列号 -源码.zip

    通过提供的标题"用JAVA读取硬盘序列号 -源码.zip"我们可以推断,这个压缩包包含了一段Java代码,用于实现这个功能。 首先,我们要理解硬盘序列号是存储在硬盘固件中的一个唯一的数字或字母组合,通常用来识别和验证...

    读取硬盘ID,找了很久才找到读取硬盘ID的代码

    return disk.SerialNumber print(get_disk_serial()) ``` 在Linux环境下,我们可以使用`hdparm`命令读取硬盘序列号,或者通过`udev`规则和`blkid`命令获取UUID。下面是一个使用bash脚本的例子: ```bash # 读取...

    读取硬盘id源程序.rar

    Console.WriteLine("Serial Number: " + disk["SerialNumber"]); } ``` 2. **Linux系统**:在Python中,可以使用`/sys/class/hdmi`目录下的文件来读取硬盘ID,如下: ```python import os with open('/sys/class/...

    java获取硬盘序列号的两种方法

    String serialNumber = result.getString("SerialNumber"); if (!serialNumber.isEmpty()) { return serialNumber; } } } catch (Exception e) { System.err.println("Error while querying WMI: " + e....

    读取硬盘ID号

    这个标识符可以是硬盘序列号(Serial Number)、硬盘型号(Model Number)或是硬盘的UUID(Universally Unique Identifier)。读取硬盘ID号是系统管理和硬件诊断的重要环节,尤其是在数据恢复、系统安装或设备追踪时...

    JAVA_program_read_hard_disk_serial_number_of_class_The Number

    JAVA读取硬盘序列号经典程序源码JAVA program to read hard disk serial number of the classical source

    Java获取CPU&主板&硬盘序列号

    硬盘序列号可以通过读取硬盘的SMART数据来获取,这通常涉及到操作系统层面的磁盘I/O操作。在Java中,可以使用`java.io.File`类和`java.nio.file.Files`类进行文件I/O操作,但这些API不直接提供获取硬盘序列号的功能...

    JAVA中获取硬盘序列号源码

    if (kernel32.GetVolumeInformationW(path, new WString(volumeNameBuffer), volumeNameBuffer.length, serialNumber, maxComponentLength, fileSystemFlags, null, 0)) { String serialNumberStr = Integer....

    windows下读取sata硬盘序列号(jni方式)

    总结,读取SATA硬盘序列号涉及到Windows API的使用,通过C++代码实现,然后利用JNI技术与Java应用程序集成。这个过程需要对操作系统底层原理、C++编程以及Java JNI有深入理解。同时,为了确保跨平台兼容性,需要为...

    查看硬盘序列号的java程序

    System.out.println("硬盘序列号: " + serialNumber); } else { System.out.println("当前Java环境不支持获取硬盘序列号"); } } } ``` 这段代码首先获取了`OperatingSystemMXBean`的实例,然后通过`...

    硬盘序列号读取diskid

    - 打开命令提示符,然后输入`wmic diskdrive get SerialNumber`。 - 这个命令会列出所有连接到系统的硬盘及其序列号。 3. **编程语言中的实现** 对于开发者来说,许多编程语言提供了API或者库来访问硬件信息。...

    java 通过jni接口获取当前机器硬盘ID(序列号)

    5. **获取硬盘序列号的本地实现**:在DLL中,可以使用Windows API如`CreateFile`、`DeviceIoControl`等函数来读取硬盘的SMART信息,从中提取序列号。也可以使用WMI查询`Win32_DiskDrive`类获取序列号。 6. **安全性...

    PB取硬盘物理ID和DES加密

    在PowerBuilder中,通过编写自定义函数或者使用系统API调用来获取硬盘物理ID,例如Windows API的`GetVolumeInformation`函数,可以读取硬盘的物理信息。 接下来,我们讨论“DES加密”。DES(Data Encryption ...

    获取硬盘序列号

    String serialNumber = DiskIDUtil.getHardDriveSerialNumber(); ``` 这里的`getHardDriveSerialNumber()`方法应当返回当前计算机上某个或所有硬盘的序列号。 4. **处理结果**:获取到硬盘序列号后,可以根据...

    获取硬盘唯一序列号和机器唯一id

    wmic diskdrive get SerialNumber ``` 这个命令将列出所有连接到计算机的硬盘的序列号。此外,还可以使用第三方软件如`DiskId32`,这是一个小巧的实用程序,专门用于显示硬盘的详细信息,包括序列号。只需运行`...

    获取硬盘序列号的C程序

    当成功执行后,输出缓冲区会包含`STORAGE_DEVICE_DESCRIPTOR`结构,其中`SerialNumber`成员就是我们所需的硬盘序列号。 5. **处理结果**:从`STORAGE_DEVICE_DESCRIPTOR`结构体中提取序列号,并以适当的形式显示在...

Global site tag (gtag.js) - Google Analytics