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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6200329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:31:55  来源:igfitidea点击:

how to load a file from folder to memory stream buffer

vb.net

提问by Ram

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.

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

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?

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

'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.

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

回答by Philipp Mueller

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)

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

然后随意使用 MemoryStream ms。只是为了 clearifyPath保存要读入内存流的完整路径和文件名。