- 浏览: 748760 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (163)
- java-Excel (2)
- java-SSH (19)
- java-SVN (2)
- java-dwr (1)
- java-Liferay (2)
- wiki-LDAP (1)
- java-jDom (1)
- LDAP (2)
- javaScript (8)
- 数据挖掘 (1)
- java-mail (1)
- java-webService (2)
- Oracle/MySql/SqlServer2k/Sybase (3)
- db-sql (3)
- 社保 (3)
- 英语资料 (1)
- 杂谈 (31)
- 设计模式 (1)
- java-webwork (1)
- java-eclipse (3)
- java-Maven (2)
- WS/SOA/ESB (1)
- java-jfreechart (1)
- 手机开发 (4)
- linux (9)
- 搜索 (1)
- Tomcat/Weblogic (6)
- CVS/Subversion (1)
- eStore (3)
- 企业家 (0)
- java-JDBC (1)
- C/C++ (3)
- Car (2)
- Dos/Shell (1)
- 算法 (2)
- English Learning (4)
- Marriage (3)
- 心灵修行 (2)
- UML及模型设计 (0)
- 数据库设计 (1)
- 资源 (1)
- 下载 (7)
- 职业之路 (4)
- 网站安全 (1)
- StateStreet (1)
- 测试 (0)
- 性能测试 (3)
- Cloud Computing (0)
- 文档管理 (0)
- 弹性云平台 (4)
- 面试必知必会 (1)
最新评论
-
forrest_lv:
博主是其中一员?
浙江大学0X级计算机和软件学院研究生就业状况 (转) -
showtimes52007:
lz实现的拷贝方法是io的,我前几天也写了个拷贝文件的方法,只 ...
文件拷贝 -
bo_hai:
总结的很好呀!谢谢呀!S
MySql用户创建、授权以及删除 -
pengzhenyi:
对于初学者来说这本书不错滴
spring_in_action_中文版 -
soundycui:
只有6-10章节
spring_in_action_中文版
Java 读取硬盘Serial Number[转]
- 博客分类:
- java-SSH
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 -->
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); } }
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); } }
发表评论
-
Http Client 访问网页。
2012-03-16 01:03 845http://hc.apache.org/httpclient ... -
jsoup posting and cookie
2012-03-16 00:48 616http://stackoverflow.com/questi ... -
使用 jsoup 对 HTML 文档进行解析和操作(比HTMLParser好)
2012-03-15 23:01 1811jsoup 简介 Java 程序在解析 HTML 文档时 ... -
Jackson 入门 【转】
2012-03-14 23:51 1782同事的一些测试结果看来,Jackson在处理Json方面性 ... -
Hibernate Call SPs
2011-04-26 21:09 1068First I am going to post the Na ... -
Struts2中文乱码问题
2008-11-25 20:44 2205Struts2中文乱码问题 有一段时间没做Struts2开发了 ... -
hibernate3.04中文文档
2008-08-01 23:20 1451hibernate3.04中文文档.rar </ta ... -
spring_in_action_中文版
2008-07-31 21:25 2121[url=../../../topics/download/e ... -
AOP和AspectJ-扫盲(转)
2008-06-06 12:18 1377原贴:http://www.jdon.com/AO ... -
appfuse学习手记
2008-03-11 13:31 18361。package com.mycompany.app.dao ... -
Struts--Ajax应用例码
2007-11-09 13:10 1454js 代码 new Element.update ... -
Struts+Spring+Hibernate实现上传下载
2007-08-29 10:54 1788下载本文源代码 ... -
Spring配置代码范例
2007-07-25 17:19 1660Spring配置代码 < ... -
加载Spring的 Web.xml配置
2007-07-25 17:12 21162xml 代码 <?xml versio ... -
Commons-logging + Log4j 入门指南
2007-07-18 15:55 1232一 :为什么同时使用commons-logging和Log4j ... -
spring -struts plugin
2007-07-16 15:23 1804struts-config.xml xml 代码 ... -
Log4j设置
2007-07-16 14:49 21451.commons-logging.properties or ... -
过滤器设置
2007-07-16 14:45 14251。SetCharacterEncodingFilter 代码 ...
相关推荐
JAVA读取硬盘序列号 java 源码 yulin_xp
java读取硬盘序列号,包括jar包。和例子。
Java读取Excel内容 v Java读取Excel内容 Java读取Excel内容
java读取TXT文件入库,java读取TXT文件入库,java读取TXT文件入库,
JAVA读取 excelJAVA读取 excel
Java读取Revit文件的C#代码
Java读取json文件,并转化为map取值,并附上相关jar包
java读取局域网共享机器文件 java读取局域网共享机器文件
JAVA读取视频时间
java读取txt文件,可以以文件路径构造这个流,:FileInputStream fin = new FileInputStream("d:/test.txt"); 然后使用这个流直接读取到文件,再使用reader构造BufferedReader,按行读取一整行的文本,作为字符串返回...
java读取文件方法大全 读取file流等技术
Java读取dwg代码演示
java读取excel数据导入数据库源码 java读取excel数据导入数据库源码
java读取mpp文件,来生成mpp文件的jar包。
使用java来读取硬盘的序列号,工作在windows下,windows xp以上有效。通过jni方式,可以参考其中的cpp 实现
通过Java读取word表格中的内容,将内容存到数据库中,将Word中的图片存到硬盘中