2013-12-18 150 views
1

我需要通過Windows CE上的VBA獲取USB驅動器的名稱。在WinCE上通過VBA獲取USB驅動器名稱

在Win 7上,USB驅動器被稱爲「我的拇指驅動器」,贏得CE認爲它是「硬盤2」。任何方式來讀取真正的拇指名稱並將其存儲在變量中?

FSO(文件系統對象)在Win CE上不可用。

----------------------編輯----------------------- ----

另一種方法是檢索txt文件的修改日期,仍然在Win CE上的VBA中。 (日期文件已創建或保存,未訪問。)

+0

USB名稱或驅動器號? –

+0

USB名稱...謝謝 – sharkyenergy

+0

增加了一個替代解決方案,我原來的職位 – sharkyenergy

回答

2

試試這個。通過文件名Sub Sample()

Option Explicit 

Private Declare Function OpenFile Lib "kernel32" _ 
(ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, _ 
ByVal wStyle As Long) As Long 

Private Declare Function CloseHandle Lib "kernel32" _ 
(ByVal hObject As Long) As Long 

Private Declare Function GetFileTime Lib "kernel32" _ 
(ByVal hFile As Long, lpCreationTime As FILETIME, _ 
lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long 

Private Declare Function FileTimeToSystemTime Lib _ 
"kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long 

Private Const OF_READ = &H0 
Private Const OFS_MAXPATHNAME = 128 

Private Type OFSTRUCT 
    cBytes As Byte 
    fFixedDisk As Byte 
    nErrCode As Integer 
    Reserved1 As Integer 
    Reserved2 As Integer 
    szPathName(OFS_MAXPATHNAME) As Byte 
End Type 

Private Type FILETIME 
    dwLowDateTime As Long 
    dwHighDateTime As Long 
End Type 

Private Type SYSTEMTIME 
    wYear As Integer 
    wMonth As Integer 
    wDayOfWeek As Integer 
    wDay As Integer 
    wHour As Integer 
    wMinute As Integer 
    wSecond As Integer 
    wMilliseconds As Integer 
End Type 

Sub Sample() 
    Debug.Print GetFileCreatedDateAndTime("c:\Sample.txt") 
End Sub 

Private Function GetFileCreatedDateAndTime(sFile As String) As String 
    Dim hFile As Long, rval As Long 
    Dim buff As OFSTRUCT 
    Dim f1Time As FILETIME, f2Time As FILETIME, f3Time As FILETIME 
    Dim stime As SYSTEMTIME 

    GetFileCreatedDateAndTime = "Unable To retrieve details" 

    hFile = OpenFile(sFile, buff, OF_READ) 
    If hFile Then 
     rval = GetFileTime(hFile, f1Time, f2Time, f3Time) 
     rval = FileTimeToSystemTime(f1Time, stime) 

     GetFileCreatedDateAndTime = _ 
     "Created on " & _ 
     stime.wDay & "-" & stime.wMonth & "-" & stime.wYear & _ 
     Chr(13) & _ 
     "Created at " & _ 
     stime.wHour & ":" & stime.wMinute & ":" & stime.wSecond 
    End If 
    rval = CloseHandle(hFile) 
End Function 
+0

測試..謝謝 – sharkyenergy

+0

我得到一個錯誤(在德國)。像你的第一個kernel32聲明 – sharkyenergy

+0

通過重新排序聲明解決的「等待一個數據類型爲例如整數」。上傳代碼到plc ..將很快報告 – sharkyenergy