Question

I'd like to load the multiple files (like Images, Documents, Pdfs) to the listview and along its properties will be displayed.

This was the code I am working with:

        FileInfo FInfo;

        DialogResult dr = this.openFD.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            // Read the files
            foreach (String file in openFD.FileNames)
            {
                string fileName = Path.GetFileNameWithoutExtension(file);
                ListViewItem item = new ListViewItem(fileName);
                item.Tag = file;

                listView1.Items.Add(item);
            }
        }

Please help me.

Was it helpful?

Solution

Here is the way I do for Excel files. You just need to modify it a bit. I am hoping this helps.

    private void loadMatchingResponsesReports()
    {
        listBox2.Items.Clear();

        string[] list = getMatchingReports();
        foreach (String S in list)
        {
            FileInfo fileResponse = new FileInfo(S);
            string fileResponseNameOnly = fileResponse.Name;
            listBox2.Items.Add(fileResponseNameOnly);
            GC.Collect();
        }
    }

    public string[] getMatchingReports()
    {
        string[] returnR = null;
        try
        {
            returnR = Directory.GetFiles(textBox3.Text + @"\", "*.xls");
        }
        catch
        {
            MessageBox.Show("Can't get some files from directory " + textBox3.Text);
        }
        return returnR;
    }

OTHER TIPS

Instead of a simple string, you might want to use a custom object to store all properties you want associated with the ListViewItem.

item.Tag = file;

file should be of custom type, a Dictionary<string, string> maybe.

You need to use the FileInfo class. For each file you want to add, construct an instance. It as has all the properties you would want to add to an explorer like interface such as: CreationTime, Extension, Name etc. You get the size (in bytes) from the Length property.

You would add a ListViewSubItem for each attribute, corresponding to the column in your ListView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top