开发者

Deleting files that are checked in a listview

开发者 https://www.devze.com 2023-02-22 10:13 出处:网络
The code below is what i have in a button I cant seem to understand how to delete these files after they are checked now understand these are actually deleting files in the temp directory that is what

The code below is what i have in a button I cant seem to understand how to delete these files after they are checked now understand these are actually deleting files in the temp directory that is what i am having issues with now deleting it from the listview i know how to remove them from the list view but the logic behind actually deleting the ones that are checked is what i am stumped on

Thank you for your help

enter code here

       if (listView1.CheckedItems.Count != 0)
        {
            // If so, loop through all checked files and delete.


            string f = ""; 
            for (int x = 0; x <= listView开发者_StackOverflow1.CheckedItems.Count - 1; x++)
            {

                    // code to delete files 
               tFile = Path.GetTempPath();
                File.Delete(file.ToString());

            }
            MessageBox.Show(f); 
        }
        else
        {
            MessageBox.Show("Please Check the files you want to delete");
        }
    }


From your comment above, I think you mean that when you call itemChecked.ToString(), the value is literally

ListViewItem: {filename}

The ListViewItem.ToString() method shows the .Text property within the brackets, so that property is what you need to reference in your loop. You might as well use a foreach loop like this:

string tempDirectory = Path.GetTempPath();
foreach (ListViewItem item in listView1.CheckedItems)
{
    string fileName = item.Text;
    string filePath = Path.Combine(tempDirectory, fileName);
    File.Delete(filePath);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消