【问题标题】:How do I get list of all filenames in a directory using VB6?如何使用 VB6 获取目录中所有文件名的列表?
【发布时间】:2008-11-15 00:19:48
【问题描述】:

在VB6中循环指定文件夹目录中的所有文件并获取它们的名称的最简单方法是什么?

【问题讨论】:

    标签: vb6 file directory


    【解决方案1】:
    sFilename = Dir(sFoldername)
    
    Do While sFilename > ""
    
      debug.print sFilename 
      sFilename = Dir()
    
    Loop
    

    【讨论】:

    【解决方案2】:
    Dim fso As New FileSystemObject
    Dim fld As Folder
    Dim fil As File
    Set fld = fso.GetFolder("C:\My Folder")
    For Each fil In fld.Files
      Debug.Print fil.Name
    Next
    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing
    

    【讨论】:

      【解决方案3】:

      DJ 的solution 简单而有效,如果您需要 FileSystemObject 可以提供的更多功能(需要参考 Microsoft Scripting Runtime),只需扔掉另一个。

      Dim fso As New FileSystemObject
      Dim fil As File
      
      For Each fil In fso.GetFolder("C:\").Files
        Debug.Print fil.Name
      Next
      

      【讨论】:

        【解决方案4】:

        '对于 VB6 非常棘手: '只需获取保存在磁盘/项目目录中的所有项目 .frm 文件的位置

        将 CountVal 调暗为整数 计数值 = 0 cbo.清除

        sFilename = Dir(App.Path & "\Forms\")
        Do While sFilename > ""
          If (Right(sFilename, 4) = ".frm") Then
          cbo.List(CountVal) = Left(sFilename, (Len(sFilename) - 4))
          CountVal = CountVal + 1
          End If
        
           sFilename = Dir()
        Loop
        

        【讨论】:

          【解决方案5】:

          创建名称为browseButton的按钮 创建名称为 List1 的文件列表框

          在设计中双击按钮

          代码应该是这样的

          Private Sub browseButton_Click()
          
          Dim path  As String
          path = "C:\My Folder"
          
          List1.path() = path
          List1.Pattern = "*.txt"
          End Sub
          

          完成现在运行它

          【讨论】:

            【解决方案6】:

            您可以使用以下演示代码,

            Dim fso As New FileSystemObject
            Dim fld As Folder
            Dim file As File
            Set fld = fso.GetFolder("C:\vishnu")
            For Each file In fld.Files
              msgbox file.Name
            Next
            

            【讨论】:

              猜你喜欢
              • 2014-09-15
              • 1970-01-01
              • 1970-01-01
              • 2016-03-07
              • 2011-03-02
              • 1970-01-01
              • 2010-12-01
              • 1970-01-01
              • 2011-03-02
              相关资源
              最近更新 更多