how to get hard disk serial number with action script
I'm using action script 3.0 compiling the code to flash 10
how to get hard disk serial number with action script?
I want to get this info without any security re开发者_如何学编程quirement. Is there a way to do it?
You could get all the physical media for a device from a C# console application and output each physical medium and its properties as an xml. Then in an Air application using the NativeApplication
API you can get said physical medium and its properties, namely the serial number for a hard disk drive. I made an example of this:
GetPhysicalMedia.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Management;
namespace GetPhysicalMedia
{
class Program
{
static void Main(string[] args)
{
ManagementClass managementClass = new ManagementClass("Win32_PhysicalMedia");
String physicalMediaXml = "";
physicalMediaXml += "<physicalMedia>\n";
foreach (ManagementObject managementObject in managementClass.GetInstances())
{
physicalMediaXml += "\t<physicalMedium ";
physicalMediaXml += "caption" + "='" + managementObject["Caption"] + "' ";
physicalMediaXml += "description" + "='" + managementObject["Description"] + "' ";
physicalMediaXml += "installDate" + "='" + managementObject["InstallDate"] + "' ";
physicalMediaXml += "name" + "='" + managementObject["Name"] + "' ";
physicalMediaXml += "status" + "='" + managementObject["Status"] + "' ";
physicalMediaXml += "creationClassName" + "='" + managementObject["CreationClassName"] + "' ";
physicalMediaXml += "manufacturer" + "='" + managementObject["Manufacturer"] + "' ";
physicalMediaXml += "model" + "='" + managementObject["Model"] + "' ";
physicalMediaXml += "sku" + "='" + managementObject["SKU"] + "' ";
physicalMediaXml += "serialNumber" + "='" + managementObject["SerialNumber"] + "' ";
physicalMediaXml += "tag" + "='" + Regex.Replace((String)managementObject["Tag"], "\\\\", "\") +"' ";
physicalMediaXml += "version" + "='" + managementObject["Version"] + "' ";
physicalMediaXml += "partNumber" + "='" + managementObject["PartNumber"] + "' ";
physicalMediaXml += "otherIdentifyingInfo" + "='" + managementObject["OtherIdentifyingInfo"] + "' ";
physicalMediaXml += "poweredOn" + "='" + managementObject["PoweredOn"] + "' ";
physicalMediaXml += "removable" + "='" + managementObject["Removable"] + "' ";
physicalMediaXml += "replaceable" + "='" + managementObject["Replaceable"] + "' ";
physicalMediaXml += "hotSwappable" + "='" + managementObject["HotSwappable"] + "' ";
physicalMediaXml += "capacity" + "='" + managementObject["Capacity"] + "' ";
physicalMediaXml += "mediaType" + "='" + managementObject["MediaType"] + "' ";
physicalMediaXml += "mediaDescription" + "='" + managementObject["MediaDescription"] + "' ";
physicalMediaXml += "writeProtectOn" + "='" + managementObject["WriteProtectOn"] + "' ";
physicalMediaXml += "cleanerMedia" + "='" + managementObject["CleanerMedia"] + "' ";
physicalMediaXml += "/>\n";
}// end foreach
physicalMediaXml += "</physicalMedia>\n";
Console.WriteLine(physicalMediaXml);
}// end method
}// end class
}// end namespace
Main.as:
package
{
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.filesystem.File;
public class Main extends Sprite
{
private var _nativeProcess:NativeProcess;
private var _nativeProcessSUI:NativeProcessStartupInfo;
private var _physicalMedia:XML;
public function Main():void
{
_nativeProcess = new NativeProcess();
_nativeProcessSUI = new NativeProcessStartupInfo();
_nativeProcessSUI.executable = File.applicationDirectory.resolvePath("GetPhysicalMedia.exe");
_nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onNativeProcessSOD);
_nativeProcess.addEventListener(Event.STANDARD_OUTPUT_CLOSE, onNativeProcessSOC);
_nativeProcess.start(_nativeProcessSUI);
}// end function
private function onNativeProcessSOD(e:ProgressEvent):void
{
_physicalMedia = XML(_nativeProcess.standardOutput.readUTFBytes(_nativeProcess.standardOutput.bytesAvailable));
}// end function
private function onNativeProcessSOC(e:Event):void
{
trace(_physicalMedia.physicalMedium.(String(@tag).search("PHYSICALDRIVE0") > -1).@serialNumber); // output: V5FEVZ2W
trace(_physicalMedia.physicalMedium.(String(@tag).search("PHYSICALDRIVE1") > -1).@serialNumber); // output: WD-WCASU2856512
}// end function
}// end class
}// end package
This is an example of the output you get for GetPhysicalMedia.exe:
<physicalMedia>
<physicalMedium caption="" description="" installDate="" name="" status="" creationClassName="" manufacturer="" model="" sku="" serialNumber="V5FEVZ2W " tag="\\.\PHYSICALDRIVE0" version="" partNumber="" otherIdentifyingInfo="" poweredOn="" removable="" replaceable="" hotSwappable="" capacity="" mediaType="" mediaDescription="" writeProtectOn="" cleanerMedia=""/>
<physicalMedium caption="" description="" installDate="" name="" status="" creationClassName="" manufacturer="" model="" sku="" serialNumber="WD-WCASU2856512 " tag="\\.\PHYSICALDRIVE1" version="" partNumber="" otherIdentifyingInfo="" poweredOn="" removable="" replaceable="" hotSwappable="" capacity="" mediaType="" mediaDescription="" writeProtectOn="" cleanerMedia=""/>
<physicalMedium caption="" description="" installDate="" name="" status="" creationClassName="" manufacturer="" model="" sku="" serialNumber="516256L411 " tag="\\.\CDROM0" version="" partNumber="" otherIdentifyingInfo="" poweredOn="" removable="" replaceable="" hotSwappable="" capacity="" mediaType="" mediaDescription="" writeProtectOn="" cleanerMedia=""/>
</physicalMedia>
There is no way to do that from FlashPlayer. You might ask for the user to type it manually the first time, use a SharedObject
and never ask again.
With AIR you can fork native OS process and communicate it to obtain HDD serial number:
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = File.applicationDirectory.resolvePath("some_hdd_utility.exe");
nativeProcessStartupInfo.executable = file;
var processArgs:Vector.<String> = new Vector.<String>();
processArgs.push("someArgs");
nativeProcessStartupInfo.arguments = processArgs;
process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.start(nativeProcessStartupInfo);
public function onOutputData(event:ProgressEvent):void
{
var stdOut:ByteArray = process.standardOutput;
var data:String = stdOut.readUTFBytes(process.standardOutput.bytesAvailable);
trace("Got: ", data);
}
Hope this helps.
精彩评论