【问题标题】:add items properties - c# winform添加项目属性 - c# winform
【发布时间】:2012-03-20 20:23:54
【问题描述】:

我想将多个文件(如图像、文档、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);
            }
        }

请帮帮我。

【问题讨论】:

  • 图片、Word 文档(excel、word、ppt 等)、PDF
  • 你的意思是属性的属性吗?比如创建日期和文件大小?
  • 是的,就像它和它的类型。就像资源管理器中显示的详细信息

标签: c# winforms fileinfo


【解决方案1】:

这是我处理 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;
    }

【讨论】:

【解决方案2】:

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

item.Tag = file;

file 应该是自定义类型,可能是 Dictionary<string, string>

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多