Hi dnnyobi,
Refer below sample.
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindFiles();
}
private void BindFiles()
{
DirectoryInfo dinfo = new DirectoryInfo(@"E:\Files");
DataTable dt = new DataTable();
dt.Columns.Add("FileName");
foreach (FileInfo file in dinfo.GetFiles())
{
dt.Rows.Add(file.Name);
}
checkedListBox1.DataSource = dt;
checkedListBox1.DisplayMember = "FileName";
checkedListBox1.ValueMember = "FileName";
}
private void OnDelete(object sender, EventArgs e)
{
foreach (object item in checkedListBox1.CheckedItems)
{
DataRowView row = item as DataRowView;
{
File.Delete(Path.Combine(@"E:\Files", row["FileName"].ToString()));
}
}
this.BindFiles();
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindFiles()
End Sub
Private Sub BindFiles()
Dim dinfo As DirectoryInfo = New DirectoryInfo("E:\Files")
Dim dt As DataTable = New DataTable()
dt.Columns.Add("FileName")
For Each file As FileInfo In dinfo.GetFiles()
dt.Rows.Add(file.Name)
Next
checkedListBox1.DataSource = dt
checkedListBox1.DisplayMember = "FileName"
checkedListBox1.ValueMember = "FileName"
End Sub
Private Sub OnDelete(sender As Object, e As EventArgs) Handles button1.Click
For Each item As Object In checkedListBox1.CheckedItems
Dim row As DataRowView = TryCast(item, DataRowView)
If True Then
File.Delete(Path.Combine("E:\Files", row("FileName").ToString()))
End If
Next
Me.BindFiles()
End Sub
Screenshot