如何在QB64中获取驱动器标签和驱动器序列号

时间:2016-01-30 05:18:45

标签: basic qbasic qb64

我最近在QB64中找到了如何获取/设置文件名属性:

DECLARE LIBRARY
    FUNCTION GetFileAttributes& (f$)
    FUNCTION SetFileAttributes& (f$, BYVAL a&)
END DECLARE

file$ = "c:\qb64\tempfile.000" + CHR$(0)

a = GetFileAttributes(file$)
a = a AND NOT 1 ' reset read-only flag
x = SetFileAttributes(file$, a)

我想知道是否有一个简单的方法 获取驱动器的卷标和序列号。

1 个答案:

答案 0 :(得分:0)

获取驱动器卷标,序列号和文件系统类型。还计算存在的驱动器,并获取驱动器类型。资料来源于QB64。

' declare external libraries.
CONST MAX_PATH = 260
DECLARE DYNAMIC LIBRARY "kernel32"
FUNCTION GetVolumeInformationA& (lpRootPathName$, _
    lpVolumeNameBuffer$, _
    BYVAL nVolumeNameSize~&, _
    lpVolumeSerialNumber~&, _
    lpMaximumComponentLength~&, _
    lpFileSystemFlags~&, _
    lpFileSystemNameBuffer$, _
    BYVAL nFileSystemNameSize&)
END DECLARE
DECLARE LIBRARY
    FUNCTION GetDriveType& (d$)
END DECLARE
DIM SHARED DriveType AS STRING
_TITLE "DRIVE LIST"
FOR Q = 1 TO 26
    X = GetFileInfo(Q)
    IF X THEN C = C + 1
NEXT
PRINT "Drives detected:"; C
END

' function gets and displays existing drive info.
FUNCTION GetFileInfo (D)
IF DRIVEEXISTS(D) THEN
    GetFileInfo = 0
    EXIT FUNCTION
END IF
COLOR 14, 0
Dname$ = CHR$(D + 64) + ":\"
PRINT "Drive: "; Dname$
Vname$ = SPACE$(MAX_PATH)
Sname$ = SPACE$(MAX_PATH)
R = GetVolumeInformationA(Dname$ + CHR$(0), Vname$, MAX_PATH, serial~&, empty1~&, empty2~&, Sname$, MAX_PATH)
' check volume mounted
IF R = 0 THEN
    PRINT "Volume: "; DriveType
    PRINT "Serial: (????-????)"
    PRINT "System: [????]"
ELSE
    ' volume label
    tmp1$ = RTRIM$(Vname$)
    v = INSTR(tmp1$, CHR$(0))
    IF v THEN tmp1$ = LEFT$(tmp1$, v - 1)
    Vname$ = tmp1$
    IF Vname$ = "" THEN Vname$ = "<none>"

    ' file system type
    tmp1$ = RTRIM$(Sname$)
    v = INSTR(tmp1$, CHR$(0))
    IF v THEN tmp1$ = LEFT$(tmp1$, v - 1)
    Fname$ = tmp1$

    ' serial number
    Sname$ = LEFT$(HEX$(serial~&), 4) + "-" + RIGHT$(HEX$(serial~&), 4)

    PRINT "Volume: "; CHR$(34) + RTRIM$(Vname$) + CHR$(34)
    PRINT "Serial: ("; Sname$; ")"
    PRINT "System: ["; RTRIM$(Fname$); "]"
END IF
GetFileInfo = -1
COLOR 15, 0
PRINT "-more-";
WHILE INKEY$ = ""
    _LIMIT 50
WEND
CLS
END FUNCTION

' check drive exists.
'  returns -1 if drive not detected.
FUNCTION DRIVEEXISTS (V)
VarX$ = CHR$(V + 64) + ":\" + CHR$(0)
VarX = GetDriveType(VarX$)
DriveType = ""
SELECT CASE VarX
    CASE 0
        DriveType = "[UNKNOWN]"
    CASE 1
        DriveType = "[BADROOT]"
    CASE 2
        DriveType = "[REMOVABLE]"
    CASE 3
        DriveType = "[FIXED]"
    CASE 4
        DriveType = "[REMOTE]"
    CASE 5
        DriveType = "[CDROM]"
    CASE 6
        DriveType = "[RAMDISK]"
END SELECT
IF VarX > 1 THEN
    DRIVEEXISTS = 0
ELSE
    DRIVEEXISTS = -1
END IF
END FUNCTION