欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

将选择的图片显示在listview中,并显示filename,path和type的简单实例

程序员文章站 2024-02-27 19:00:03
复制代码 代码如下:if (openfiledialog1.showdialog() == dialogresult.ok)     ...

复制代码 代码如下:

if (openfiledialog1.showdialog() == dialogresult.ok)
           {
               listview1.items.clear();
               string[] files = openfiledialog1.filenames; //定义一个数组,获取选择的文件
               string[] fileinfo = new string[3];  //定义一个数组,用于存储文件信息
               for (int i = 0; i < files.length; i++)  //遍历文件数组
               {
                   string path = files[i].tostring();  //获取文件路径
                   //截取文件名字
                   string filename = path.substring(path.lastindexof("\\") + 1, path.length - 1 - path.lastindexof("\\"));
                   //截取文件类型
                   string filetype = filename.substring(filename.lastindexof(".") + 1, filename.length - 1 - filename.lastindexof("."));
                   fileinfo[0] = filename;
                   fileinfo[1] = path;
                   fileinfo[2] = filetype;
                   listviewitem lvi = new listviewitem(fileinfo);
                   listview1.items.add(lvi);

               }
          }

注释:listview1, view 属性为details

openfiledialog1,multiselect属性为true