在winXp下能获取MAC地址,在Vista系统下怎么获取不到啊?!

yrenyuan 2009-05-20 10:22:57
try {

Enumeration<NetworkInterface> el = NetworkInterface.getNetworkInterfaces();

while (el.hasMoreElements()) {

NetworkInterface ni = el.nextElement();
// MACを取得する
byte[] mac = ni.getHardwareAddress();

if (mac == null) {
continue;
}

builder = new StringBuilder();

for (byte b : mac) {
builder.append(hexByte(b));

// 16進法の文字列に"-"が追加する
builder.append("-");
}

// 指定文字列を削除する
builder.deleteCharAt(builder.length() - 1);

System.out.println("@@@@@@@@1==" + ni.isUp() + ":" + builder.toString().toUpperCase());
System.out.println("#########==" + ni.getDisplayName());
String strDisplayName = ni.getDisplayName();
int index = strDisplayName.indexOf(strWireless);

// ネットワークアダプタが使用中またWireLess以外の場合
if (ni.isUp() && index == -1) {
rtnMac = builder.toString().toUpperCase();
System.out.println("$$$$$$$$$==" + rtnMac);
break;
}
}
} catch (Exception exception) {
exception.printStackTrace();
}

return rtnMac;
}
...全文
181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yrenyuan 2009-09-10
  • 打赏
  • 举报
回复
由于不同操作系统读取MAC地址的命令、以及命令输出格式的不同,所以该程序不能直接运用到其它系统。要移植到其它系统只需针对命令的输出格式进行修改。
yrenyuan 2009-09-02
  • 打赏
  • 举报
回复
由于不同操作系统读取MAC地址的命令、以及命令输出格式的不同,所以该程序不能直接运用到其它系统。要移植到其它系统只需针对命令的输出格式进行修改。

我可以利用先去操作系统的标志,在判断是什么操作系统后,做分支,利用各自的方法去MAC
yrenyuan 2009-09-02
  • 打赏
  • 举报
回复
因为在XP 和 vista下面,运行dos,所显示的物理地址的(MAC地址)长度不一样。所以,长度分别是36和32的。
marcblue 2009-05-20
  • 打赏
  • 举报
回复
public static String getMac() throws IOException
{
String command="ipconfig /all";
String mac=null;
Process proc=Runtime.getRuntime().exec(command);
BufferedReader reader=new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line=null;
while((line=reader.readLine())!=null)
{
if(line.indexOf("Physical Address")!=-1)
{
mac=line.substring(line.indexOf("Physical Address")+36, line.length());
break;
}
}
return mac;
}


Vista下需要改成:

if(line.indexOf(”物理地址”)!=-1)
{

mac=line.substring(line.indexOf(”物理地址”)+32, line.length());
break;

}
marcblue 2009-05-20
  • 打赏
  • 举报
回复
由于不同操作系统读取MAC地址的命令、以及命令输出格式的不同,所以该程序不能直接运用到其它系统。要移植到其它系统只需针对命令的输出格式进行修改。
brehme2007 2009-05-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 marcblue 的回复:]
Java codepublic static String getMac() throws IOException
{
String command="ipconfig /all";
String mac=null;
Process proc=Runtime.getRuntime().exec(command);
BufferedReader reader=new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line=null;
while((line=reader.readLine())!=null)
{
if(line.indexOf("Physical Address")!=-1)
{
mac=line.substring(line.indexOf("Physica…
[/Quote]
请问在winxp和vista下为啥要在读取索引后各加上36,和32.不明白。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧