C# - How To Fill ComboBox With Files Names Using C#
In This C# Tutorial We Will See How To Bind A Combobox With Files Names
In CSharp Programming Language.
In CSharp Programming Language.
Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form11 : Form
{
public Form11()
{
InitializeComponent();
}
private void Form11_Load(object sender, EventArgs e)
{
string path = @"C:\Users\Directory\Folder";
DataTable table = new DataTable();
table.Columns.Add("File Name");
table.Columns.Add("File Path");
string[] files = Directory.GetFiles(path);
for(int i = 0; i < files.Length; i++)
{
FileInfo file = new FileInfo(files[i]);
table.Rows.Add(file.Name, path + "\\" + file.Name);
}
// using foreach loop
DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo fileInf in dir.GetFiles())
{
table.Rows.Add(fileInf.Name, path + "\\" + fileInf.Name);
}
comboBox1.DataSource = table;
comboBox1.DisplayMember = "File Name";
comboBox1.ValueMember = "File Path";
}
}
}