1

我想将多个文件(如图像、文档、Pdfs)加载到列表视图中,并将显示其属性。

这是我正在使用的代码:

        FileInfo FInfo;

        DialogResult dr = this.openFD.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            // Read the files
            foreach (String file in openFD.FileNames)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);
                ListViewItem item = new ListViewItem(fileName);
                item.Tag = file;

                listView1.Items.Add(item);
            }
        }

请帮我。

4

3 回答 3

3

这是我处理 Excel 文件的方法。你只需要稍微修改一下。我希望这会有所帮助。

    private void loadMatchingResponsesReports()
    {
        listBox2.Items.Clear();

        string[] list = getMatchingReports();
        foreach (String S in list)
        {
            FileInfo fileResponse = new FileInfo(S);
            string fileResponseNameOnly = fileResponse.Name;
            listBox2.Items.Add(fileResponseNameOnly);
            GC.Collect();
        }
    }

    public string[] getMatchingReports()
    {
        string[] returnR = null;
        try
        {
            returnR = Directory.GetFiles(textBox3.Text + @"\", "*.xls");
        }
        catch
        {
            MessageBox.Show("Can't get some files from directory " + textBox3.Text);
        }
        return returnR;
    }
于 2012-03-20T20:39:22.593 回答
0

您需要使用FileInfo类。对于要添加的每个文件,构造一个实例。它具有您想要添加到类似界面的资源管理器的所有属性,例如:CreationTime、Extension、Name 等。您可以从Length属性中获取大小(以字节为单位)。

您将为每个属性添加一个ListViewSubItem,对应于 ListView 中的列。

于 2012-03-20T22:36:07.967 回答
0

您可能希望使用自定义对象来存储您希望与 ListViewItem 关联的所有属性,而不是简单的字符串。

item.Tag = file;

file应该是自定义类型,Dictionary<string, string>也许。

于 2012-03-20T20:38:42.973 回答