public void LoadListViewByMyPcPath(ListView listView, ImageList imageList, string myPcPath) { Cursor.Current = Cursors.WaitCursor; ListView.ListViewItemCollection listViewItems = listView.Items; ImageList.ImageCollection imageListItems = imageList.Images; listViewItems.Clear(); imageListItems.Clear(); string[] dirsArray; int imageIndex = 0; try { dirsArray = Directory.GetLogicalDrives(); for (int i = 0; i < dirsArray.Length; i++) { DriveInfo driInfo = new DriveInfo(dirsArray[i]); string[] driInfoArray = new string[4]; driInfoArray[0] = string.Format("{0}({1})", driInfo.VolumeLabel, driInfo.Name); driInfoArray[1] = SFUCommon.GetCommonSize(driInfo.TotalSize); driInfoArray[2] = SFUCommon.GetCommonSize(driInfo.TotalFreeSpace); driInfoArray[3] = driInfo.Name; ListViewItem listViewItem = new ListViewItem(driInfoArray, imageIndex); listViewItems.Add(listViewItem); imageListItems.Add(IconUtil.GetDirectoryIcon(driInfo.Name)); imageIndex++; } } catch (Exception) { //如果不使用try catch抓取错误并以此跳过错误,在添加没有放入光盘的光驱Items时会提示错误 } Cursor.Current = Cursors.Default; }
public void LoadListView(ListView listView, ImageList imageList, string remotePath) { if (upYun == null) { return; } Cursor.Current = Cursors.WaitCursor; List <FolderItem> itemsArray = new List <FolderItem>(); try { itemsArray = upYun.readDir(remotePath).Cast <FolderItem>().ToList(); } catch { } // UpYun SDK BUG: 调用readDir方法时,如果该目录下没有任何文件或目录,会有数组越界的问题 try { ListView.ListViewItemCollection listViewItems = listView.Items; int imageIndex = 0; string programPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); ImageList.ImageCollection imageItems = imageList.Images; listViewItems.Clear(); imageItems.Clear(); if (!remotePath.Equals("/")) { string[] itemInfo = new string[4]; itemInfo[0] = "上级目录"; itemInfo[3] = "ParentDir"; ListViewItem parentDirItem = new ListViewItem(itemInfo, 0); listViewItems.Add(parentDirItem); imageItems.Add(StaticFileUpload.Business.Properties.Resources.up_16x16); imageIndex++; } imageItems.Add(IconUtil.GetDirectoryIcon(programPath)); imageIndex++; foreach (FolderItem item in itemsArray) { ListViewItem listViewItem = null; string[] itemInfo = new string[4]; itemInfo[0] = item.filename; itemInfo[1] = SFUCommon.GetCommonSize(item.size); itemInfo[2] = SFUCommon.GetCommonTime(item.number).ToString(); if (item.filetype.Equals("N")) { itemInfo[3] = "N"; listViewItem = new ListViewItem(itemInfo, imageIndex); imageItems.Add(IconUtil.GetFileIcon(item.filename, false)); imageIndex++; } else { itemInfo[3] = "F"; listViewItem = new ListViewItem(itemInfo, remotePath.Equals("/") ? 0 : 1); } listViewItems.Add(listViewItem); } } catch (Exception ex) { SFULogger.DEFAULT.Error(ex); throw; } Cursor.Current = Cursors.Default; }
public void LoadListView(ListView listView, ImageList imageList, string localPath) { Cursor.Current = Cursors.WaitCursor; string[] filesArray, diresArray; int imageIndex = 1; ListView.ListViewItemCollection listViewItems = listView.Items; ImageList.ImageCollection imageListItems = imageList.Images; try { diresArray = Directory.GetDirectories(localPath); filesArray = Directory.GetFiles(localPath); } catch (Exception ex) { SFULogger.DEFAULT.Error("该目录不存在!", ex); MessageBox.Show("该目录不存在!"); return; } // 清空 ListView & ImageList 控件条目集合 listViewItems.Clear(); imageListItems.Clear(); listViewItems.Add("上级目录", 0); imageListItems.Add(StaticFileUpload.Business.Properties.Resources.up_16x16); // 遍历 localPath 目录下的子文件夹 for (int i = 0; i < diresArray.Length; i++) { string[] dirInfoArray = new string[3]; DirectoryInfo directoryInfo = new DirectoryInfo(diresArray[i]); if ((directoryInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) { dirInfoArray[0] = directoryInfo.Name; dirInfoArray[1] = " "; dirInfoArray[2] = directoryInfo.LastWriteTime.ToString(); ListViewItem listViewItem = new ListViewItem(dirInfoArray, imageIndex); listViewItems.Add(listViewItem); imageListItems.Add(dirInfoArray[0], IconUtil.GetDirectoryIcon(directoryInfo.FullName)); imageIndex++; } } // 遍历 localPath 目录下的子文件 for (int i = 0; i < filesArray.Length; i++) { string[] fileInfoArray = new string[3]; FileInfo fileInfo = new FileInfo(filesArray[i]); if ((fileInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) { fileInfoArray[0] = fileInfo.Name; fileInfoArray[1] = SFUCommon.GetCommonSize(fileInfo.Length); fileInfoArray[2] = fileInfo.LastWriteTime.ToString(); ListViewItem listViewItem = new ListViewItem(fileInfoArray, imageIndex); listViewItems.Add(listViewItem); imageListItems.Add(fileInfo.Name, IconUtil.GetFileIcon(fileInfo.FullName, false)); imageIndex++; } } Cursor.Current = Cursors.Default; }