public void LoadFiles(string dirpath) { if (Directory.Exists(dirpath)) { listView1.BeginUpdate(); path = dirpath; fileSystemWatcher1.Path = dirpath; DirectoryInfo di = new DirectoryInfo(dirpath); FileInfo[] fi = di.GetFiles("*.htm"); listView1.Items.Clear(); for (int i = 0; i < fi.Length; i++) { string fileName1 = dirpath + "\\" + Path.GetFileNameWithoutExtension(fi[i].Name); ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(fi[i].Name)); lvi.SubItems.Add(dirpath + "\\" + fi[i].Name); lvi.SubItems.Add(fi[i].CreationTime.ToShortDateString() + " " + fi[i].CreationTime.ToShortTimeString()); lvi.SubItems.Add(fi[i].LastWriteTime.ToShortDateString() + " " + fi[i].LastWriteTime.ToShortTimeString()); lvi.SubItems.Add(FileCore.BytesToString(fi[i].Length)); lvi.SubItems.Add(fi[i].Length.ToString()); listView1.Items.Add(lvi); listView1.Items[i].SubItems[4].Tag = "Number"; } listView1.EndUpdate(); } }
/// <summary> /// 将一个文件的的名称添加到LISTVIEW中文件要存在的 /// C:\123.html /// </summary> /// <param name="filename"></param> public void AddItem(string filename) { //listView1.HideSelection = false; listView1.Focus(); FileInfo file = new FileInfo(filename); //添加名称 ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(filename)); lvi.SubItems.Add(filename);//FILENAME lvi.SubItems.Add(file.CreationTime.ToShortDateString() + " " + file.CreationTime.ToShortTimeString()); lvi.SubItems.Add(file.LastWriteTime.ToShortDateString() + " " + file.LastWriteTime.ToShortTimeString()); lvi.SubItems.Add(FileCore.BytesToString(file.Length));//大小 lvi.Tag = file.Length; listView1.Items.Add(lvi); //选中添加的ITEM int i = listView1.Items.IndexOf(lvi); listView1.EnsureVisible(i); listView1.MultiSelect = false; listView1.Items[i].Selected = true; //focusedListViewItem1 = listView1.Items[i]; listView1.MultiSelect = true; //lstCls1.AddItem(filename); /// selfilename = filename; }
/// <summary> /// 搜索的时候添加用的 /// C:\123.html /// </summary> /// <param name="filename"></param> public void AddSearchItem(string filename) { FileInfo file = new FileInfo(filename); //添加名称 ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(filename)); // + Path.GetExtension(filename) lvi.SubItems.Add(filename); //FILENAME lvi.SubItems.Add(file.CreationTime.ToShortDateString() + " " + file.CreationTime.ToShortTimeString()); lvi.SubItems.Add(file.LastWriteTime.ToShortDateString() + " " + file.LastWriteTime.ToShortTimeString()); lvi.SubItems.Add(FileCore.BytesToString(file.Length));//大小 listView1.Items.Add(lvi); }
public void AddItem(string filename, int imageIndex) { //listView1.HideSelection = false; listView1.Focus(); FileInfo file = new FileInfo(filename); //添加名称 ListViewItem lvi = new ListViewItem(Path.GetFileName(filename)); lvi.ImageIndex = imageIndex; lvi.SubItems.Add(filename); //FILENAME lvi.SubItems.Add(FileCore.BytesToString(file.Length)); //大小 lvi.Tag = file.Length; listView1.Items.Add(lvi); }
// 添加一个目录下的子目录和文件(仅一级) public void LoadData(string path) { try { if (!Directory.Exists(workpath)) { Directory.CreateDirectory(workpath); } listView1.Items.Clear(); Win32.SetImageListData(listView1); //获得一个目录下的子文件夹 不包括文件夹下的文件夹 string[] sub1 = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly); for (int i = 0; i < sub1.Length; i++) { DirectoryInfo d = new DirectoryInfo(sub1[i]); ListViewItem lvi = new ListViewItem(d.Name, Win32.FileIconIndex(sub1[i])); lvi.SubItems.Add(d.LastWriteTime.ToShortDateString() + " " + d.LastWriteTime.ToShortTimeString()); lvi.SubItems.Add(""); lvi.SubItems.Add(path + "\\" + d.Name); lvi.SubItems.Add("文件夹"); listView1.Items.Add(lvi); } DirectoryInfo di = new DirectoryInfo(path); FileInfo[] fi = di.GetFiles("*", SearchOption.TopDirectoryOnly); for (int i = 0; i < fi.Length; i++) { string s = Path.GetExtension(fi[i].Name); if (s.Length > 1) { s = s.Remove(0, 1);//去掉扩展名中的 . } ListViewItem lvi = new ListViewItem(fi[i].Name, Win32.FileIconIndex(path + "\\" + fi[i].Name)); lvi.SubItems.Add(fi[i].LastWriteTime.ToShortDateString() + " " + fi[i].LastWriteTime.ToShortTimeString()); lvi.SubItems.Add(FileCore.BytesToString(fi[i].Length)); lvi.SubItems.Add(path + "\\" + fi[i].Name); lvi.SubItems.Add(s + " 文件"); listView1.Items.Add(lvi); } if (listView1.Items.Count > 0) { listView1.Items[0].Selected = true; } for (int i = 0; i < listView1.Items.Count; i++) { //http://stackoverflow.com/questions/7620120/listview-subitems-font-not-working listView1.Items[i].UseItemStyleForSubItems = false; listView1.Items[i].SubItems[1].ForeColor = Color.Gray; listView1.Items[i].SubItems[2].ForeColor = Color.Gray; listView1.Items[i].SubItems[3].ForeColor = Color.Gray; listView1.Items[i].SubItems[4].ForeColor = Color.Gray; } } catch (System.Exception ex) { MessageBox.Show("附件 " + ex.Message); } }