listView1.Items.RemoveAt(0);
string[] arrfilePathlist; // Select Folder/Directory private void btnBrowse_Click(object sender, EventArgs e) { // Clear text box txtPath.Clear(); txtPath.Text = String.Empty; FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.ShowNewFolderButton = false; folderDialog.RootFolder = Environment.SpecialFolder.MyComputer; folderDialog.Description = "Select the directory that you want to use"; if (folderDialog.ShowDialog() == DialogResult.OK) { txtPath.Text = folderDialog.SelectedPath; // Use Dirctory.GetFiles method to get all files in directory // and, store in an array. arrfilePathlist = Directory.GetFiles(folderDialog.SelectedPath, "*.*"); lblFileCount.Text = arrfilePathlist.Length.ToString(); listView1.Items.RemoveAt(0); // Display items in the ListView control for(int i = 0; i < arrfilePathlist.Length; i++) { listView1.Items.Add(Path.GetFileName(arrfilePathlist[i])); //listView1.Items[0].SubItems.Add(Path.GetFileName(arrfilePathlist[i])); } }
using System; using System.Windows.Forms; using System.IO; namespace FolderContent { public partial class frmMain : Form { public frmMain() { InitializeComponent(); lblFileCount.Text = String.Empty; } string[] arrfilePathlist; // Select Folder/Directory private void btnBrowse_Click(object sender, EventArgs e) { // Clear text box txtPath.Clear(); txtPath.Text = String.Empty; FolderBrowserDialog folderDialog = new FolderBrowserDialog(); folderDialog.ShowNewFolderButton = false; folderDialog.RootFolder = Environment.SpecialFolder.MyComputer; folderDialog.Description = "Select the directory that you want to use"; if (folderDialog.ShowDialog() == DialogResult.OK) { txtPath.Text = folderDialog.SelectedPath; // Use Dirctory.GetFiles method to get all files in directory // and, store in an array. arrfilePathlist = Directory.GetFiles(folderDialog.SelectedPath, "*.*"); lblFileCount.Text = arrfilePathlist.Length.ToString(); //uncomment next line to remove first empty row. //listView1.Items.RemoveAt(0); // Display items in the ListView control for(int i = 0; i < arrfilePathlist.Length; i++) { listView1.Items.Add(Path.GetFileName(arrfilePathlist[i])); //listView1.Items[0].SubItems.Add(Path.GetFileName(arrfilePathlist[i])); } } else { return; } // populate listview control with array. } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)