Hi Evoteam21,
Please refer below sample.
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BindDataGrid();
}
private void btnUpload_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "Folder1";
openFileDialog1.DefaultExt = "*.*";
openFileDialog1.FilterIndex = 6;
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFileDialog1.Multiselect = true;
string saveDirectory = @"E:\Files\";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (!Directory.Exists(saveDirectory))
{
Directory.CreateDirectory(saveDirectory);
}
foreach (string item in openFileDialog1.FileNames)
{
string fileName = Path.GetFileName(openFileDialog1.FileName);
string fileSavePath = Path.Combine(saveDirectory, fileName);
File.Copy(openFileDialog1.FileName, fileSavePath, true);
}
}
BindDataGrid();
}
private void BindDataGrid()
{
string[] filePaths = Directory.GetFiles("E:\\Files\\");
DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");
foreach (string filePath in filePaths)
{
dt.Rows.Add(Path.GetFileName(filePath), filePath);
}
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Clear();
DataGridViewColumn name = new DataGridViewTextBoxColumn();
name.Name = "Name";
name.HeaderText = "File Name";
name.DataPropertyName = "Text";
name.Width = 100;
dataGridView1.Columns.Insert(0, name);
DataGridViewColumn path = new DataGridViewTextBoxColumn();
path.Name = "Path";
path.HeaderText = "File Path";
path.DataPropertyName = "Value";
path.Width = 100;
dataGridView1.Columns.Insert(1, path);
dataGridView1.DataSource = dt;
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "";
buttonColumn.Width = 60;
buttonColumn.Name = "buttonColumn";
buttonColumn.Text = "Delete";
buttonColumn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Insert(2, buttonColumn);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if (MessageBox.Show(string.Format("Do you want to delete Name:{0}?", row.Cells["Name"].Value), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Delete(row.Cells["Path"].Value.ToString());
BindDataGrid();
}
}
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
BindDataGrid()
End Sub
Private Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
OpenFileDialog1.FileName = "Folder1"
OpenFileDialog1.DefaultExt = "*.*"
OpenFileDialog1.FilterIndex = 6
OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
OpenFileDialog1.Multiselect = True
Dim saveDirectory As String = "E:\Files\"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
If Not Directory.Exists(saveDirectory) Then
Directory.CreateDirectory(saveDirectory)
End If
For Each item As String In OpenFileDialog1.FileNames
Dim fileName As String = Path.GetFileName(OpenFileDialog1.FileName)
Dim fileSavePath As String = Path.Combine(saveDirectory, fileName)
File.Copy(OpenFileDialog1.FileName, fileSavePath, True)
Next
End If
BindDataGrid()
End Sub
Private Sub BindDataGrid()
Dim filePaths As String() = Directory.GetFiles("E:\Files\")
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Text")
dt.Columns.Add("Value")
For Each filePath As String In filePaths
dt.Rows.Add(System.IO.Path.GetFileName(filePath), filePath)
Next
dataGridView1.AllowUserToAddRows = False
dataGridView1.Columns.Clear()
Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn()
name.Name = "Name"
name.HeaderText = "File Name"
name.DataPropertyName = "Text"
name.Width = 100
dataGridView1.Columns.Insert(0, name)
Dim path As DataGridViewColumn = New DataGridViewTextBoxColumn()
path.Name = "Path"
path.HeaderText = "File Path"
path.DataPropertyName = "Value"
path.Width = 100
dataGridView1.Columns.Insert(1, path)
dataGridView1.DataSource = dt
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
buttonColumn.HeaderText = ""
buttonColumn.Width = 60
buttonColumn.Name = "buttonColumn"
buttonColumn.Text = "Delete"
buttonColumn.UseColumnTextForButtonValue = True
dataGridView1.Columns.Insert(2, buttonColumn)
End Sub
Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
If e.ColumnIndex = 2 Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
If MessageBox.Show(String.Format("Do you want to delete Name:{0}?", row.Cells("Name").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
File.Delete(row.Cells("Path").Value.ToString())
BindDataGrid()
End If
End If
End Sub
End Class
Screenshot