bluemoon 发表于 2003-12-3 09:24:00

请教各位大侠 如何打开一个空的文件目录啊?

本帖最后由 作者 于 2003-12-3 11:07:17 编辑

请问如何打开一个空的文件目录啊?用CommonDialog 的showopen 属性只能带开含有文件的目录啊
另:如何判断一个文件夹是否为空啊?

efan2000 发表于 2003-12-3 12:45:00

这是一个Window中使用的目录浏览器。

Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
End Type
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'KPDTeam@Allapi.net
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
      'Set the owner window
      .hWndOwner = Me.hWnd
      'lstrcat appends the two strings and returns the memory address
      .lpszTitle = lstrcat("C:\", "")
      'Return only if the user selected a directory
      .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    'Show the 'Browse for folder' dialog
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
      sPath = String$(MAX_PATH, 0)
      'Get the path from the IDList
      SHGetPathFromIDList lpIDList, sPath
      'free the block of memory
      CoTaskMemFree lpIDList
      iNull = InStr(sPath, vbNullChar)
      If iNull Then
            sPath = Left$(sPath, iNull - 1)
      End If
    End If

    MsgBox sPath
End Sub


关于文件夹和文件的操作,可以在VB或者VBA中引用Microsoft Scripting Runtime类型库,它提供了一些比较简单的方法来操作。
如:

Sub test()
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    '判断文件夹是否存在
    Debug.Print fso.FolderExists("c:\test")
    '判断文件是否存在
    Debug.Print fso.FileExists("c:\test")
    '判断文件夹中子文件夹的数目
    Debug.Print fso.GetFolder("c:\test").SubFolders.Count
    '判断文件夹中文件的数目
    Debug.Print fso.GetFolder("c:\test").Files.Count
    Set fso = Nothing
End Sub

其它的用法可以参考VBA帮助。

bluemoon 发表于 2003-12-3 16:21:00

感谢,非常!!
页: [1]
查看完整版本: 请教各位大侠 如何打开一个空的文件目录啊?