Code for js to call activeX to obtain the serial number of a USB flash drive

Source: Internet
Author: User

Copy codeThe Code is as follows:
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! = ":")
{
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 (); // stores the serial number
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 ("your USB flash series:" + uSerialNum );
}
}
}
Else
{
Alert ("insert a USB flash drive ");
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.