js调用activeX获取u盘序列号的代码
作者:
js调用activeX获取u盘序列号的代码,需要的朋友可以参考下。
复制代码 代码如下:
function GetDIVORPIV(DIV)
{
var str;
var arrayDIV=new Array();
if(DIV.indexOf("_")>0)
{
arrayDIV=DIV.split("_");
str=arrayDIV[1];
}
else
{
str="";
}
return str;
}
function GetDIVandPIV(DIVPIV)
{
var strDIVandPIV;
var strDIVPIV=DIVPIV;
var arrayDIVPIV=new Array();
if(strDIVPIV.indexOf("&")>0)
{
arrayDIVPIV=strDIVPIV.split("&");
strDIVandPIV=GetDIVORPIV(arrayDIVPIV[0])+GetDIVORPIV(arrayDIVPIV[1]);
}
else
{
strDIVandPIV="";
}
return strDIVandPIV;
}
function GetUsbSN(DIVPIVSN)
{
var UsbSN;
var strSN=DIVPIVSN ;
var arraySN=new Array();
if(strSN.indexOf("&")>0)
{
arraySN=strSN.split("\\");
if(arraySN.length>2)
{
UsbSN=GetDIVandPIV(arraySN[1])+arraySN[2];
}
else
{
UsbSN="";
}
}
else
{
UsbSN="";
}
return UsbSN;
}
function CheckUsb()
{
var e,x;
var ForReading = 1,RorWriting = 2;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var bfResult=false ;
e = new Enumerator(fso.Drives)
for(;!e.atEnd();e.moveNext())
{
x=e.item();
if(x.DriveType==1)
{
if(x.Path!="A:")
{
bfResult=true ;
}
}
}
return bfResult;
}
function ReadUsbSN()
{
var bfUsb;
bfUsb=CheckUsb();
if(bfUsb==true)
{
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var computer = locator.ConnectServer(".");
var properties = computer.ExecQuery("SELECT * FROM Win32_USBHub");
var e = new Enumerator (properties);
var arrayUsbSN=new Array();//存放序列号
var intCount=0;
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
var strSN;
var uSerialNum;
var sn=p.DeviceID
if(sn.indexOf("VID")>0)
{
strSN=GetUsbSN(sn);
uSerialNum=strSN.substring(8);
arrayUsbSN[intCount]=strSN;
intCount=intCount+1;
alert("你的U盘系列号为:"+ uSerialNum);
}
}
}
else
{
alert("请插入U盘");
}
}