You can get all the
filename from any directory. We can also get their names, sizes, then
we can operate any condition to add/remove files from the datatable, in
which we store all the files. In the below example we show all the files
in Gridview
public void LoadUploadedFiles(ref GridView gv)
{
DataTable dtFiles =new DataTable();
if (Session["dt"] != null)
{
dtFiles = (DataTable)Session["dt"];
DataTable dtFiles1 = GetFilesInDirectory(HttpContext.Current.Server.MapPath(foldername));
}
gv.DataSource = dtFiles;
gv.DataBind();
}
public DataTable GetFilesInDirectory(string sourcePath)
{
System.Data.DataTable dtFiles = new System.Data.DataTable();
if ((Directory.Exists(sourcePath)))
{
dtFiles.Columns.Add(new System.Data.DataColumn("Name"));
dtFiles.Columns.Add(new System.Data.DataColumn("Size"));
dtFiles.Columns["Size"].DataType = typeof(double);
dtFiles.Columns.Add(new System.Data.DataColumn("ConvertedSize"));
DirectoryInfo dir = new DirectoryInfo(sourcePath);
foreach (FileInfo files in dir.GetFiles("*.*"))
{
System.Data.DataRow drFile = dtFiles.NewRow();
drFile["Name"] = files.Name;
drFile["Size"] = files.Length;
drFile["ConvertedSize"] = CalculateFileSize(files.Length);
dtFiles.Rows.Add(drFile);
}
}
return dtFiles;
}
public string CalculateFileSize(double FileInBytes)
{
string strSize = "00";
if (FileInBytes < 1024)
strSize = FileInBytes + " B";//Byte
else if (FileInBytes > 1024 & FileInBytes < 1048576)
strSize = Math.Round((FileInBytes / 1024), 2) + " KB";//Kilobyte
else if (FileInBytes > 1048576 & FileInBytes < 107341824)
strSize = Math.Round((FileInBytes / 1024) / 1024, 2) + " MB";//Megabyte
else if (FileInBytes > 107341824 & FileInBytes < 1099511627776)
strSize = Math.Round(((FileInBytes / 1024) / 1024) / 1024, 2) + " GB";//Gigabyte
else
strSize = Math.Round((((FileInBytes / 1024) / 1024) / 1024) / 1024, 2) + " TB";//Terabyte
return strSize;
}
public void LoadUploadedFiles(ref GridView gv)
{
DataTable dtFiles =new DataTable();
if (Session["dt"] != null)
{
dtFiles = (DataTable)Session["dt"];
DataTable dtFiles1 = GetFilesInDirectory(HttpContext.Current.Server.MapPath(foldername));
}
gv.DataSource = dtFiles;
gv.DataBind();
}
public DataTable GetFilesInDirectory(string sourcePath)
{
System.Data.DataTable dtFiles = new System.Data.DataTable();
if ((Directory.Exists(sourcePath)))
{
dtFiles.Columns.Add(new System.Data.DataColumn("Name"));
dtFiles.Columns.Add(new System.Data.DataColumn("Size"));
dtFiles.Columns["Size"].DataType = typeof(double);
dtFiles.Columns.Add(new System.Data.DataColumn("ConvertedSize"));
DirectoryInfo dir = new DirectoryInfo(sourcePath);
foreach (FileInfo files in dir.GetFiles("*.*"))
{
System.Data.DataRow drFile = dtFiles.NewRow();
drFile["Name"] = files.Name;
drFile["Size"] = files.Length;
drFile["ConvertedSize"] = CalculateFileSize(files.Length);
dtFiles.Rows.Add(drFile);
}
}
return dtFiles;
}
public string CalculateFileSize(double FileInBytes)
{
string strSize = "00";
if (FileInBytes < 1024)
strSize = FileInBytes + " B";//Byte
else if (FileInBytes > 1024 & FileInBytes < 1048576)
strSize = Math.Round((FileInBytes / 1024), 2) + " KB";//Kilobyte
else if (FileInBytes > 1048576 & FileInBytes < 107341824)
strSize = Math.Round((FileInBytes / 1024) / 1024, 2) + " MB";//Megabyte
else if (FileInBytes > 107341824 & FileInBytes < 1099511627776)
strSize = Math.Round(((FileInBytes / 1024) / 1024) / 1024, 2) + " GB";//Gigabyte
else
strSize = Math.Round((((FileInBytes / 1024) / 1024) / 1024) / 1024, 2) + " TB";//Terabyte
return strSize;
}