復制代碼 代碼如下:
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