将多个文件读入列表视图 VB.Net [英] Read Multiple Files Into List View VB.Net

查看:23
本文介绍了将多个文件读入列表视图 VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要要求这个程序的用户从任何目录中选择两个文本文件.然后我需要在我构建的列表视图中显示它们.文件 1 需要加载到第一列,文件 2 需要加载到第二列.它们将相互对应.

I need to require the user of this program to select two text files from any directory. I then need to display them in a List View, which I have built. File 1 needs to load into the first column and File 2 needs to load into the second. They will correspond to each other.

我目前有以下允许多选

OpenFileDialog.Multiselect = True

我遇到的问题是将这些独特的文件分成相应的列.例如,以下代码非常有效地将第一个文件的内容加载到第一列中:

What I am having trouble with is separating these unique files into corresponding columns. For example, the following code very effectively loads the first file's contents into the first column:

        Dim fileName As String = OpenFileDialog.FileName

        fileReader = New StreamReader(fileName)

        Do While fileReader.Peek() <> -1
        firstFile = fileReader.ReadLine & vbNewLine
        ListView1.Items.Add(firstFile)

        Loop

当我选择第二个文件时,第一个文件的内容在同一列中被第二个文件的内容替换.

When I choose the second file, the first file's contents are replaced within the same column by the second file's contents.

我曾考虑使用数组,但不确定如何将唯一文件加载到每个索引中.

I have looked at using an array, but am unsure how to load the unique files into each index.

我真的不知道该从哪里开始.

I'm not really sure where to go from here.

推荐答案

当你使用

OpenFileDialog1.Multiselect = true

所有选中的文件已经作为集合存储在OpenFileDialog1.FileNames中,只需遍历所有值并将它们放入列表视图

All selected files are already stored as a collection in OpenFileDialog1.FileNames, just loop through all values and put them into the listview

ListView1.Items.Clear
Dim file as string
For Each file in OpenFileDialog1.FileNames
    ListView1.Item.Add(file)
Next

如果您想在不同的列中显示文件内容,那么您可能需要稍微更改一下代码

if you want to show the file content in different columns, then you may need to change a little bit of your code

    Dim fileName As String = OpenFileDialog.FileName

    fileReader = New StreamReader(fileName)

    Dim FileItem As New ListViewItem
    Do While fileReader.Peek() <> -1
        firstFile = fileReader.ReadLine & vbNewLine

        FileItem .SubItem.Add(firstFile)

    Loop
    ListView1.item.add(Item)

但是,在添加任何项目之前,您可能需要在 ListView1 中声明列.如果您的 ListView1 中没有定义列,那么即使您将文件内容放入 subItem 中,也无法显示列

However you may need to declare the columns in the ListView1 before adding any item. If not columns define in your ListView1, then it is not possible to show the columns even you have put your file content into the subItem

这篇关于将多个文件读入列表视图 VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆