I mam having problems doing somthing fairly simple. ;) . What i have is a listview with about 1000 items in it. All files paths that i have scanned from a directory. And there are 4 colums. First the name, then the size, extesion ect. the last is the path to the file. Right now the user can click to copy them and they are all copied to a differ location. Thats easy....they can also choose to just copy the one checked one. Thats easy to i used this

try
            {
                string s = Registry.CurrentUser.OpenSubKey(@"Software\ManagePod\GeneralConfig", true).GetValue("CopyDir").ToString();   
                string mp3 = listViewFiles.SelectedItems[0].SubItems[3].Text.ToString();
                MP3File mp3File = ShellID3TagReader.ReadID3Tags(mp3);
                File.Copy(mp3, s + "\\" + mp3File.SongTitle + "-" + mp3File.ArtistName + ".mp3");
            }
            catch
            {

                //error handling
            }

Now what i want to be able to do is let the user copy any files that are Checked.. So say they check off six files. On a button click i want the six checked files copied to the target location. Now i no this should be easy. But i am strugling witht the subitems part. I have somthing like this.

//so here is some puesdo code
foreach(4th column of every checked item)//the 4th column is that path
{
File.Copy(the path of each 4th selected item, target loaction
}

Thanks for any help

-T

Dani AI

Generated

The symptom you saw ("ListViewItem: {thepath}") comes from calling the subitem object itself (or its ToString()), not the cell text. was right to point out that the displayed string lives on the subitem's text property — that change is what fixed the immediate problem for .

A few tips and safer patterns to avoid similar trouble going forward:

  • Keep a canonical full path off the UI columns by storing it in the ListViewItem.Tag when you populate the list. That avoids relying on a hardcoded column index and keeps the visible columns free for formatted values.
  • Build destination paths with Path.Combine, sanitize any tag/ID3-derived filenames (remove invalid filename chars), check that the source exists, and create the target directory before copying.
  • Do per-file try/catch and log failures instead of letting the whole batch fail. For long copy operations run the work off the UI thread (BackgroundWorker, Task.Run) and report progress.

Example pattern (store path once, then copy checked items):

// when creating the row: keep the full path in Tag
var row = new ListViewItem(displayName);
row.SubItems.Add(sizeString);
row.SubItems.Add(ext);
row.SubItems.Add(folder); // optional visible column
row.Tag = fullFilePath;
listViewFiles.Items.Add(row);

// later: gather checked sources and copy with basic validation
var targetDir = targetRoot;
Directory.CreateDirectory(targetDir);

var sources = listViewFiles.Items
    .Cast<ListViewItem>()
    .Where(it => it.Checked)
    .Select(it => it.Tag as string)
    .Where(p => !string.IsNullOrEmpty(p));

foreach (var src in sources)
{
    if (!File.Exists(src)) { /* log missing */ continue; }
    var name = MakeSafeFileName(Path.GetFileName(src));
    var dest = Path.Combine(targetDir, name);
    try { File.Copy(src, dest, true); }
    catch (Exception ex) { /* record which file failed and why */ }
}

static string MakeSafeFileName(string s)
{
    foreach (var c in Path.GetInvalidFileNameChars()) s = s.Replace(c, '_');
    return s;
}

If the list is large, perform copies asynchronously and throttle concurrency. Also, when using ID3 tags for filenames, always fallback to the original filename if tags are missing or contain illegal characters.

Recommended Answers

All 4 Replies

foreach (ListViewItem lvi in this.listViewFiles.CheckedItems)
			{
// do whatever here
				string s =  lvi.SubItems[3];
			}
foreach (ListViewItem lvi in this.listViewFiles.CheckedItems)
			{
// do whatever here
				string s =  lvi.SubItems[3];
			}

Thanks. But i hd tried somthing similar to that and it failed. So did yours. See when i do the copy functin it says that path is not supported. so using this code

foreach (ListViewItem lvi in this.listViewFiles.CheckedItems)
            {
                string s = lvi.SubItems[3].ToString();
                MessageBox.Show(s);       
            }

I had a messagebox show me it, it looks like this

ListViewItem: {thepath}

thats not going to work. Any ideas?

btw this is the code i tried to use to copy it. but because of the format it didnt work.

string s1 = Registry.CurrentUser.OpenSubKey(@"Software\ManagePod\GeneralConfig", true).GetValue("CopyDir").ToString();   //this is an entry in th e reg it is the path to a dir. this case the dir where it will be copied to.
            foreach (ListViewItem lvi in this.listViewFiles.CheckedItems)
            {
                string s = lvi.SubItems[3].ToString();
                MP3File mp3File = ShellID3TagReader.ReadID3Tags(s);//class to read MP3's
                File.Copy(s, s1 + "\\" +  mp3File.SongTitle + "-" + mp3File.ArtistName + ".mp3");
                
            }

-T

lvi.SubItems[3].Text

Thanks that did it :)

-T

P.s that was easy :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.