java获取win硬盘序列号等信息

获取磁盘序列号

在java下获取windows系统中的磁盘序列号,废话少说直接上代码。

/**
     * 获取windows硬盘序列号
     * @param drive
     * @return 硬盘序列号
     */
    public static String getWinHardiskSn(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";
    	      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){
    	    	if(Config.DEBUG){
					e.printStackTrace();
				}
    	    }
    	    return result.trim();
    	  }
在以上代码中使用了cscript工具获取。CScript 是小型C语言 编译器,它的语法和C语言语法很接近,因此 CScript 的 源代码也可以用其他 C/C++编译器进行编译,例如Visual C++.CScript 可以生成 中间代码机器码.机器码可以在 CScript IDE 中直接运行,也可以用来生成 EXE 可执行文件.CScript 支持调用DLL和COM(Component Object Model),它可以用来编写OpenGL 和 DirectX 程序.另外您可以使用CScript 编程接口在您自己的程序中增加对C语言 脚本的支持。

以上是在windows系统中获取磁盘序列好的方法,在linux系统中执行

hdparm -i /dev/sda1

命令获取磁盘序列号,不过要root权限。在windows系统不涉权限问题(但有些杀毒软件和防火墙会对程序进行拦截)。


获取磁盘大小信息

磁盘大小信息是不涉及权限问题的。

/**
    * 获取win磁盘信息
    * @return win磁盘信息列表
    */
   public static ArrayList<HashMap<String, String>> getWinHardDiskInfo() {
	   HashMap<String, String> hashMap = null;
	   ArrayList<HashMap<String, String>> diskList = new ArrayList<HashMap<String,String>>();
	   File[] roots = File.listRoots();
	   for (File file : roots) {
		 hashMap = new HashMap<String, String>();
	     hashMap.put("disk", file.getPath());
	     hashMap.put("total",Long.toString(file.getTotalSpace()));
	     hashMap.put("free", Long.toString(file.getFreeSpace()));
	     //hashMap.put("sn", getWinHardiskSn(file.getPath()));
	     diskList.add(hashMap);
	     }
	   return diskList;
   }

在unix系统中可以执行
Runtime.getRuntime().exec("df");
df命令获取然后再java中解析字符信息可以获取到,这里不上code了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取本机CPU序列号硬盘序列号需要使用系统相关的API,因此代码会有一定的平台依赖性。以下是获取这两个序列号Java代码示例: 获取CPU序列号: ```java public static String getCpuSerial() { String result = ""; try { Process process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" }); process.getOutputStream().close(); Scanner scanner = new Scanner(process.getInputStream()); scanner.next(); result = scanner.next(); scanner.close(); } catch (IOException e) { e.printStackTrace(); } return result.trim(); } ``` 获取硬盘序列号: ```java public static String getHardDiskSerial() { String result = ""; try { File file = File.createTempFile("realhowto", ".vbs"); file.deleteOnExit(); FileWriter fw = new FileWriter(file); String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n" + "Set colDrives = objFSO.Drives\n" + "Set objDrive = colDrives.item(\"C\")\n" + "Wscript.Echo objDrive.SerialNumber"; fw.write(vbs); fw.close(); Process process = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); process.getOutputStream().close(); Scanner scanner = new Scanner(process.getInputStream()); result = scanner.next(); scanner.close(); } catch (IOException e) { e.printStackTrace(); } return result.trim(); } ``` 需要注意的是,以上代码只是演示了如何获取CPU序列号硬盘序列号,实际应用中还需要进行一些错误处理和适配不同的操作系统。同时,获取硬盘序列号需要在Windows系统中运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值