如何将文件从文件夹加载到内存流缓冲区

如何将文件从文件夹加载到内存流缓冲区

问题描述:

我正在开发 vb.net win 表单.我的任务是将文件夹中的文件名显示到 gridview 控件上.当用户在我的 UI 中单击处理按钮时,gridview 中存在的所有文件名,相应的文件必须一个接一个地加载到内存流缓冲区中,并将标题附加到文件的内容中,并使用 _ed 将其保存在硬盘驱动器中文件名的后缀.

I am working on vb.net win form. My task is display the file names from a folder onto gridview control. when user clicks process button in my UI, all the file names present in gridview, the corresponding file has to be loaded onto memory stream buffer one after another and append the titles to the content of the file and save it in hard drive with _ed as a suffix to the file name.

我是非常基础的程序员.我已经做了以下尝试并成功地将文件名显示到 gridview 上.但不知道后面的部分.请问有什么建议吗?

I am very basic programmer. I have done the following attempt and succeeded in displaying filenames onto gridview. But no idea of later part. Any suggestions please?

'将文件夹中的文件显示到网格视图上

'Displaying files from a folder onto a gridview

    Dim inqueuePath As String = "C:\Users\Desktop\INQUEUE"
    Dim fileInfo() As String
    Dim rowint As Integer = 0
    Dim name As String
    Dim directoryInfo As New System.IO.DirectoryInfo(inqueuePath)
    fileInfo = System.IO.Directory.GetFiles(inqueuePath)

    With Gridview1
        .Columns.Add("Column 0", "FileName")
        .AutoResizeColumns()
    End With

    For Each name In fileInfo
        Gridview1.Rows.Add()
        Dim filename As String = System.IO.Path.GetFileName(name)
        Gridview1.Item(0, rowint).Value = filename
        rowint = rowint + 1
    Next

非常感谢您抽出宝贵的时间阅读这篇文章.

Thank you very much for spending your valuable time to read this post.

将文件读入内存流非常容易,只需查看以下示例,您应该能够将其转换为适合您的需求:

to read a file into a memorystream is quite easy, just have a look at the following example and you should be able to convert it to suite your needs:

    Dim bData As Byte()
    Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(Path))
    bData = br.ReadBytes(br.BaseStream.Length)
    Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)
    ms.Write(bData, 0, bData.Length)

然后随意使用 MemoryStream ms.只是为了清除 Path 包含您想要读入内存流的完整路径和文件名.

then just use the MemoryStream ms as you please. Just to clearify Path holds the full path and filename you want to read into your memorystream.