private void tsmi_dgv_new_items_Click(object sender, EventArgs e) { string _path; ToolStripMenuItem tsmi = (ToolStripMenuItem)sender; if (IOComm.PathCreate(this, (PathType)tsmi.Tag, _CurrentPath, out _path)) { FileInfo _fi = new FileInfo(_path); if (_fi.Exists) { DataGridViewRow _row = dataGridView.Rows[dataGridView.Rows.Add()]; _row.Cells[nameColumn.Index].Value = _fi.Name; _row.Cells[typeColumn.Index].Value = _fi.Extension; _row.Cells[typeColumn.Index].Tag = PathType.File; _row.Cells[lengthColumn.Index].Value = string.Empty; _row.Cells[creattimeColumn.Index].Value = FileHelper.LengthFormat(_fi.Length); _row.Cells[icoColumn.Index].Value = IOComm.GetIcon(_fi.Extension); } else { DirectoryInfo _di = new DirectoryInfo(_path); DataGridViewRow _row = dataGridView.Rows[dataGridView.Rows.Add()]; _row.Cells[nameColumn.Index].Value = _di.Name; _row.Cells[typeColumn.Index].Value = "文件夹"; _row.Cells[typeColumn.Index].Tag = PathType.Floder; _row.Cells[lengthColumn.Index].Value = string.Empty; _row.Cells[creattimeColumn.Index].Value = _di.CreationTime; _row.Cells[icoColumn.Index].Value = IOComm.FloderIcon; } } }
//获取主列表 private void _loadDirectory() { Action _action; DirectoryInfo _di = null; DataGridViewRow _row; try { _di = new DirectoryInfo(_DirLoading); _CurrentDirs = _di.GetDirectories(); _CurrentFiles = _di.GetFiles(); } catch (Exception ex) { _action = delegate { MessageBox.Show(this, ex.Message, "读取错误", MessageBoxButtons.OK); }; Invoke(_action); return; } _CurrentPath = _DirLoading;//更新当前显示的路径 #region 导航栏更新 _action = delegate { dataGridView.Rows.Clear(); pathComboBox.Items.Clear(); pathComboBox.Items.Add(_CurrentPath); pathComboBox.Text = _CurrentPath; }; Invoke(_action); #endregion #region 加载文件夹 foreach (DirectoryInfo item in _CurrentDirs) { _action = delegate { _row = dataGridView.Rows[dataGridView.Rows.Add()]; _row.Cells[nameColumn.Index].Value = item.Name; _row.Cells[typeColumn.Index].Value = "文件夹"; _row.Cells[typeColumn.Index].Tag = PathType.Floder; _row.Cells[lengthColumn.Index].Value = string.Empty; _row.Cells[creattimeColumn.Index].Value = item.CreationTime; }; Invoke(_action); } #endregion #region 加载文件列表 IEnumerable <FileInfo> _query; if (_CurrentExts.Contains(".*")) { _query = _CurrentFiles; } else { _query = from FileInfo fi in _CurrentFiles where _CurrentExts.FindIndex(t => string.Compare(fi.Extension, t, true) == 0) != -1 select fi; } foreach (FileInfo item in _query) { _action = delegate { _row = dataGridView.Rows[dataGridView.Rows.Add()]; _row.Cells[nameColumn.Index].Value = item.Name; _row.Cells[typeColumn.Index].Value = item.Extension; _row.Cells[typeColumn.Index].Tag = PathType.File; _row.Cells[lengthColumn.Index].Value = FileHelper.LengthFormat(item.Length); _row.Cells[creattimeColumn.Index].Value = item.CreationTime; }; Invoke(_action); } //当前查看所有文件类型 #endregion #region 分组委托加载文件图标 PathType ff = PathType.Unknown; foreach (DataGridViewRow item in dataGridView.Rows) { _action = delegate { ff = (PathType)item.Cells[typeColumn.Index].Tag; if (ff == PathType.File) { item.Cells[icoColumn.Index].Value = IOComm.GetIcon(item.Cells[typeColumn.Index].Value?.ToString()); } else if (ff == PathType.Floder) { item.Cells[icoColumn.Index].Value = IOComm.FloderIcon; } else { item.Cells[icoColumn.Index].Value = IOComm.DefaultIcon; } }; Invoke(_action); } #endregion //刷新列表后清空选择的数据 _CurrentName = null; dataGridView.ClearSelection(); _CurrentFf = PathType.Floder; }