You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Basic中按磁盘物理顺序列出文件夹内文件

在VB.NET中按磁盘物理存储顺序列出文件

实现思路

.NET自带的FileIO.FileSystem.GetFiles或Directory.GetFiles方法默认按字母排序,没有直接获取物理存储顺序的选项。要实现需求,需调用Windows原生API的FindFirstFile和FindNextFile函数——这两个函数返回的文件顺序和DOS的DIR命令一致,对应文件在文件系统主文件表(MFT)中的记录顺序,也就是你设备播放时采用的物理存储顺序。

具体实现

1. 声明Windows API相关结构体与函数

在VB.NET中先声明所需的API函数和结构体,用于遍历目录并获取文件信息:

Imports System.Runtime.InteropServices
Imports System.IO

Public Class FilePhysicalOrderHelper
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
    Private Structure WIN32_FIND_DATA
        Public dwFileAttributes As UInteger
        Public ftCreationTime As FILETIME
        Public ftLastAccessTime As FILETIME
        Public ftLastWriteTime As FILETIME
        Public nFileSizeHigh As UInteger
        Public nFileSizeLow As UInteger
        Public dwReserved0 As UInteger
        Public dwReserved1 As UInteger
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
        Public cFileName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)>
        Public cAlternateFileName As String
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Private Structure FILETIME
        Public dwLowDateTime As UInteger
        Public dwHighDateTime As UInteger
    End Structure

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function FindFirstFile(ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function FindNextFile(ByVal hFindFile As IntPtr, ByRef lpFindFileData As WIN32_FIND_DATA) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)>
    Private Shared Function FindClose(ByVal hFindFile As IntPtr) As Boolean
    End Function

2. 编写物理顺序文件遍历方法

添加公共方法,传入文件夹路径即可返回按物理顺序排列的文件列表:

Public Shared Function GetFilesInPhysicalOrder(folderPath As String) As List(Of String)
        Dim filesList As New List(Of String)()
        Dim findData As New WIN32_FIND_DATA()
        Dim searchPath As String = Path.Combine(folderPath, "*")

        ' 初始化文件查找
        Dim hFind As IntPtr = FindFirstFile(searchPath, findData)
        If hFind = IntPtr.Zero Then Return filesList

        Try
            Do
                ' 跳过当前目录(.)和上级目录(..)
                If findData.cFileName <> "." AndAlso findData.cFileName <> ".." Then
                    filesList.Add(Path.Combine(folderPath, findData.cFileName))
                End If
            Loop While FindNextFile(hFind, findData)
        Finally
            ' 关闭查找句柄,释放资源
            FindClose(hFind)
        End Try

        Return filesList
    End Function
End Class

3. 使用示例

在程序中调用上述方法,即可获取并查看文件的物理存储顺序:

Sub Main()
    Dim targetFolder As String = "C:\YourMusicFolder" ' 替换为你的目标文件夹路径
    Dim physicalOrderFiles As List(Of String) = FilePhysicalOrderHelper.GetFilesInPhysicalOrder(targetFolder)

    Console.WriteLine("文件物理存储顺序:")
    For Each filePath In physicalOrderFiles
        Console.WriteLine(Path.GetFileName(filePath))
    Next
End Sub

注意事项

  • 该方法返回的顺序与DIR命令完全一致,匹配设备播放时的物理顺序
  • 支持NTFS、FAT32等常见Windows文件系统
  • 需确保程序对目标文件夹有读取权限,避免查找失败

内容的提问来源于stack exchange,提问作者Wally

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 21:07:39