Hi counterkin,
Please refer below updated code.
HTML
<asp:Label ID="lblShow" runat="server"></asp:Label>
Namespaces
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindData_folder();
}
}
private void BindData_folder()
{
string folderName = "1001";
string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Uploads/");
List<ListItem> directories = new List<ListItem>();
foreach (string directoryPath in Directory.GetDirectories(path))
{
directories.Add(new ListItem(new DirectoryInfo(directoryPath).Name, GetDirectorySize(directoryPath)));
}
lblShow.Text = string.Format("File size of Folder <b>{0}</b> is {1}.", folderName, directories.Where(x => x.Text == folderName).FirstOrDefault().Value);
}
private string GetDirectorySize(string directoryPath)
{
long size = 0;
DirectoryInfo di = new DirectoryInfo(directoryPath);
FileInfo[] files = di.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
size += file.Length;
}
return (size / (double)(1024 * 1024)).ToString("F2") + " MB";
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindData_folder()
End If
End Sub
Private Sub BindData_folder()
Dim folderName As String = "1001"
Dim path As String = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath & "/Uploads/")
Dim directories As List(Of ListItem) = New List(Of ListItem)()
For Each directoryPath As String In Directory.GetDirectories(path)
directories.Add(New ListItem(New DirectoryInfo(directoryPath).Name, GetDirectorySize(directoryPath)))
Next
lblShow.Text = String.Format("File size of Folder <b>{0}</b> is {1}.", folderName, directories.Where(Function(x) x.Text = folderName).FirstOrDefault().Value)
End Sub
Private Function GetDirectorySize(ByVal directoryPath As String) As String
Dim size As Long = 0
Dim di As DirectoryInfo = New DirectoryInfo(directoryPath)
Dim files As FileInfo() = di.GetFiles("*", SearchOption.AllDirectories)
For Each file As FileInfo In files
size += file.Length
Next
Return (size / CDbl((1024 * 1024))).ToString("F2") & " MB"
End Function
Screenshot