void lvMain_SubItemEndEditing(object sender, ListViewEx.SubItemEndEditingEventArgs e) { if (e.Cancel) { return; } try { string resultText = e.DisplayText; if (ei_.HasResult) { e.DisplayText = resultText = ei_.Result; } LVInfo lvinfo = (LVInfo)e.Item.Tag; LinkData lnk = new LinkData(lvinfo.FullName, this); string column = GetColumnName(e.SubItem); if (column == COLUMN_NAME) { try { string from = lvinfo.FullName; string to = Path.Combine(lvinfo.ParentDir, e.DisplayText + ".lnk"); System.IO.File.Move(from, to); e.Item.Tag = new LVInfo(to); } catch (Exception ex) { Alert(ex.Message); } } else if (column == COLUMN_PATH) { lnk.Path = resultText; } else if (column == COLUMN_ARGUMENTS) { lnk.Arguments = resultText; } else if (column == COLUMN_WORKINGDIRECTORY) { lnk.WorkingDirectory = resultText; } else if (column == COLUMN_ICONPATH) { lnk.IconPath = resultText; } else if (column == COLUMN_ICONINDEX) { lnk.IconIndex = int.Parse(resultText); } else if (column == COLUMN_RUNASADMIN) { lnk.IsRunAsAdmin = bool.Parse(resultText); } else { Debug.Assert(false); } } catch (Exception ex) { Alert(ex.Message); } UpdateItem(e.Item); e.DisplayText = e.Item.SubItems[e.SubItem].Text; }
private void UpdateList(bool setNumber) { if (setNumber) { //DirectoryInfo di = new System.IO.DirectoryInfo(CurrentInventoryFolder); //FileInfo[] fis = di.GetFiles("*.*", SearchOption.TopDirectoryOnly); //int currentMaxNumber = 0; //foreach (FileInfo fi in fis) //{ // if (IsNumbered(fi.Name)) // { // int num = GetNumber(fi.Name); // currentMaxNumber = Math.Max(currentMaxNumber, num); // } //} int newNumber = 0; List <string> moveFroms = new List <string>(); List <string> moveTos = new List <string>(); string dir = null; foreach (ListViewItem item in lvMain.Items) { LVInfo info = (LVInfo)item.Tag; Debug.Assert(dir == null || dir == info.ParentDir); dir = info.ParentDir; string oldName = info.FileName; string newName = info.FileName; if (IsNumbered(newName)) { newName = UnsetNumber(newName); } newName = SetNumber(++newNumber, newName); if (oldName != newName) { moveFroms.Add(Path.Combine(dir, oldName)); moveTos.Add(Path.Combine(dir, newName)); } } Debug.Assert(moveFroms.Count == moveTos.Count); if (moveFroms.Count != 0) { int ret = CppUtils.MoveFiles(moveFroms.ToArray(), moveTos.ToArray()); if (ret != 0 && ret != 128) { Alert(Properties.Resources.FAILED_TO_MOVE_FILES); return; } } } lvMain.Items.Clear(); { DirectoryInfo di = new System.IO.DirectoryInfo(CurrentInventoryFolder); System.IO.FileInfo[] fis = di.GetFiles("*.*", SearchOption.TopDirectoryOnly); Array.Sort(fis, delegate(FileInfo f1, FileInfo f2) { return(f1.Name.CompareTo(f2.Name)); } ); bool colored = false; foreach (FileInfo fi in fis) { if (!fi.IsReadOnly && (fi.Attributes & FileAttributes.Hidden) == 0 && string.Compare(fi.Extension, ".lnk", true) == 0) { ListViewItem item = new ListViewItem(); item.Tag = new LVInfo(fi.FullName); UpdateItem(item); item.BackColor = colored ? option_.btnLVColor1.BackColor : option_.btnLVColor2.BackColor; colored = !colored; lvMain.Items.Add(item); } } } }
void UpdateItem(ListViewItem item) { LVInfo info = (LVInfo)item.Tag; item.Text = Path.GetFileNameWithoutExtension(info.FileName); { NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); IntPtr himl = NativeMethods.SHGetFileInfo(info.FullName, 128, //FileAttribute.Normal, ref shfi, (uint)Marshal.SizeOf(shfi), 16 | //SHGFI_USEFILEATTRIBUTES NativeMethods.SHGFI_DISPLAYNAME | NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON | 0 ); Debug.Assert(himl == hSysImgList); // should be the same imagelist as the one we set item.ImageIndex = shfi.iIcon; } LinkData lnk = new LinkData(info.FullName, this); foreach (ColumnHeader ch in lvMain.Columns) { if (ch.Name == COLUMN_NAME) { continue; } ListViewItem.ListViewSubItem sub = item.SubItems[ch.Name]; if (sub == null) { sub = new ListViewItem.ListViewSubItem(); sub.Name = ch.Name; item.SubItems.Add(sub); } if (ch.Name == COLUMN_PATH) { sub.Text = lnk.Path; } else if (ch.Name == COLUMN_ARGUMENTS) { sub.Text = lnk.Arguments; } else if (ch.Name == COLUMN_WORKINGDIRECTORY) { sub.Text = lnk.WorkingDirectory; } else if (ch.Name == COLUMN_ICONPATH) { sub.Text = lnk.IconPath; } else if (ch.Name == COLUMN_ICONINDEX) { sub.Text = lnk.IconIndex.ToString(); } else if (ch.Name == COLUMN_RUNASADMIN) { sub.Text = lnk.IsRunAsAdmin.ToString(); } else { Debug.Assert(false); } } //if(lvMain.Items.Count!=0) // lvMain.RedrawItems(0, lvMain.Items.Count-1, false); }