private void AddViewItem(ListItemInfo listItemInfo, bool isfile = true) { ListViewItem li = new ListViewItem(); li.Text = listItemInfo.Name; li.SubItems.Add(listItemInfo.CrtTm.ToString("yyyy-MM-dd HH:MM:ss")); li.SubItems.Add(listItemInfo.ModTm.ToString("yyyy-MM-dd HH:MM:ss")); li.SubItems.Add(listItemInfo.Type); long size = listItemInfo.Size; li.SubItems.Add(Util.FormatSize(size)); long largeFile = 1024 * 1024 * 1024; if (size > largeFile) { li.BackColor = Color.Red; } if (isfile) { li.ForeColor = Color.Green; string extension = Util.GetExtension(listItemInfo.Name); if (extension != "") { li.ImageIndex = Util.GetFileExtesionIndex(extension); } else { li.ImageIndex = (int)ImageFileType.FILE; } } else { if (size == 0) { li.ForeColor = Color.Blue; } li.ImageIndex = (int)ImageFileType.DIRECTORY; } listView1.Items.Add(li); }
private void RightMenuItemClick(object sender, EventArgs e) { if (sender is ToolStripItem) { ToolStripItem toolStripItem = (ToolStripItem)sender; var items = listView1.SelectedItems; foreach (var item in items) { ListViewItem li = (ListViewItem)item; bool isDirec = false; DirectoryInfo selectDirectoryInfo = null; FileInfo selectFileInfo = null; DirectoryInfo di = new DirectoryInfo(CurSelectNode.FullPath); DirectoryInfo[] dis = di.GetDirectories(); foreach (var directory in dis) { if (directory.Name == li.Text) { isDirec = true; selectDirectoryInfo = directory; break; } } if (!isDirec) { FileInfo[] fis = di.GetFiles(); foreach (var fileInfo in fis) { if (fileInfo.Name == li.Text) { selectFileInfo = fileInfo; break; } } } switch (toolStripItem.Text) { case "属性": if (isDirec) { MessageBox.Show("详细属性:\n" + "名称:" + selectDirectoryInfo.Name + "\n" + "类型:文件夹\n" + "位置:" + selectDirectoryInfo.FullName + "\n" + "大小:" + Util.FormatSize(Util.GetDirectoryLength(selectDirectoryInfo.FullName)) + "\n" + "创建时间:" + selectDirectoryInfo.CreationTime.ToString("yyyy年MM月dd日,HH:MM:ss") + "\n" + "最后修改时间:" + selectDirectoryInfo.LastWriteTime.ToString("yyyy年MM月dd日,HH:MM:ss") + "\n" + "最后访问时间:" + selectDirectoryInfo.LastAccessTime.ToString("yyyy年MM月dd日,HH:MM:ss"), selectDirectoryInfo.Name + "属性", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("详细属性:\n" + "名称:" + selectFileInfo.Name + "\n" + "类型:" + selectFileInfo.Extension.Replace(".", "").ToUpper() + "文件(" + selectFileInfo.Extension + ")" + "\n" + "位置:" + selectFileInfo.FullName + "\n" + "大小:" + Util.FormatSize(Util.GetDirectoryLength(selectFileInfo.FullName)) + "\n" + "创建时间:" + selectFileInfo.CreationTime.ToString("yyyy年MM月dd日,HH:MM:ss") + "\n" + "最后修改时间:" + selectFileInfo.LastWriteTime.ToString("yyyy年MM月dd日,HH:MM:ss") + "\n" + "最后访问时间:" + selectFileInfo.LastAccessTime.ToString("yyyy年MM月dd日,HH:MM:ss"), selectFileInfo.Name + "属性", MessageBoxButtons.OK, MessageBoxIcon.Information); } break; case "删除": if (isDirec) { DialogResult dr = MessageBox.Show("确定是否删除文件夹" + selectDirectoryInfo.Name + "?", "文件夹删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == DialogResult.OK) { try { selectDirectoryInfo.Delete(); } catch (Exception ee) { MessageBox.Show(ee.Message, "删除失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { DialogResult dr = MessageBox.Show("确定是否删除文件" + selectFileInfo.Name + "?", "文件删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == DialogResult.OK) { try { selectFileInfo.Delete(); } catch (Exception ee) { MessageBox.Show(ee.Message, "删除失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { RefreshViewList(CurSelectNode.TreeNode); } } } break; } } } }
private void TreeViewAfterSelect(object sender, TreeViewEventArgs e) { TreeNode node = e.Node; if (node.FullPath == "我的电脑")//当前选择了我的电脑根目录 { currentFolderType = FolderType.COMPUTER; InitListViewDrive(); listView1.Items.Clear(); DriveInfo[] allDirves = DriveInfo.GetDrives(); //检索计算机上的所有逻辑驱动器名称 foreach (DriveInfo item in allDirves) { //Fixed 硬盘 //Removable 可移动存储设备,如软盘驱动器或USB闪存驱动器。 if (item.IsReady) { string driveName = string.Format("{0} ({1})", item.VolumeLabel, item.Name.Replace("\\", "")); ListViewItem li = new ListViewItem(); li.Text = driveName; li.ImageIndex = (int)ImageFileType.DRIVE; li.SubItems.Add(item.DriveFormat); li.SubItems.Add(Util.FormatSize(item.TotalSize)); li.SubItems.Add(Util.FormatSize(item.TotalSize - item.TotalFreeSpace)); li.SubItems.Add(Util.FormatSize(item.TotalFreeSpace)); if (item.TotalFreeSpace * 10 < item.TotalSize)//剩余空间不足10%背景显示红色 { li.BackColor = Color.Red; } listView1.Items.Add(li); } else { Console.Write("没有就绪"); } } } else { InitListView(); bool isdriveRoot = false; DriveInfo[] allDirves = DriveInfo.GetDrives(); //检索计算机上的所有逻辑驱动器名称 foreach (DriveInfo item in allDirves) { string driveName = string.Format("{0} ({1})", item.VolumeLabel, item.Name.Replace("\\", "")); if (node.Text == driveName) { currentFolderType = FolderType.Driver; AddListItemByFullPath(item.Name); isdriveRoot = true; break; } } if (!isdriveRoot && treeNodeDic.ContainsKey(node.FullPath) && (CurSelectNode == null || CurSelectNode.TreeNode != node)) { CurSelectNode = treeNodeDic[node.FullPath]; currentFolderType = FolderType.DIRECTORY; AddListItem(node); } } }