一直想办法使用QTP获取本机硬盘的ID,试了很多种方法,都不是很理想。今天终于找到一种可以行之有效的方法了,分享一下。
思路:
1、通过QTP操作DOS窗口
2、从DOS窗口输入获取硬盘ID的命令
3、最后获取DOS窗口的结果
关键代码如下:
SystemUtil.Run "CMD.exe","","C:\WINDOWS\system32"
Set currentWindow = Window( "object class:=ConsoleWindowClass","index:=0" )
currentWindow.Type "diskpart"
currentWindow.Type micReturn
wait 2
currentWindow.Type "select disk=0"
currentWindow.Type micReturn
wait 2
currentWindow.Type "detail disk"
currentWindow.Type micReturn
wait 2
visibleText = currentWindow.GetVisibleText()
currentWindow.Close
i = instr(1,visibleText,"ID")
hardDiskID = MID(visibleText,i+3,12)
msgbox Trim(hardDiskID)