Search code examples
c#winformsfileinfo

add items properties - c# winform


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.


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;
        }