如何在ListViewControl上获取项目和子项目? [英] How to get the item and subItem on the ListViewControl?

查看:52
本文介绍了如何在ListViewControl上获取项目和子项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问ListViewControl中的文件?在初始事件中,我有一个按钮,该按钮使用foreach循环列出了lsitView上的必要文件.该控件有2个项目:文件名和文件名.文件路径(子项)

How to access the files in ListViewControl? On an initial event, I have a button that lists the necessary files on the lsitView using foreach loop. The control has 2 items: File Name & File Path (subItem)

DirectoryInfo d = new DirectoryInfo(path);
foreach (FileInfo f in d.GetFiles("*.txt", SearchOption.AllDirectories))
{
    lstProjectFiles.Items.Add(f.Name).SubItems.Add(f.DirectoryName.ToString());
}      

现在,我想显示每个项目的完整路径以及带有扩展名的文件名.

Now, I would like to display each items' full path plus the filename with extensions.

foreach(ListViewItem f in lstView1.Items)
{
    Console.Writeline(f.ToString());
}

当我得到文件名时,输出如下.有多余的字符串,而没有filename.extension:

When I get the filename, the output is like below. There are extra string and not the filename.extension only:

ListViewItem:{sample.txt}

ListViewItem: {sample.txt}

推荐答案

尝试类似的方法,它使用的是SubItems

Try something like this, it is using the SubItems Text Property.

foreach (ListViewItem f in lstProjectFiles.Items)
{
    Console.WriteLine(f.SubItems[0].Text);
    Console.WriteLine(f.SubItems[1].Text);
}

这篇关于如何在ListViewControl上获取项目和子项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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