I want to display the folder and file size in list view which is placed in my form.
Is it possible to achieve the folder size at the whole(including sub folders and files) and display it from remote computer?
With the following code, i can get the original file size, but am not all getting original folder size. Instead of it, am getting folder size as 0kb
.
ManagementScope ManagementScope1 = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", strIP), options);
ManagementScope1.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ObjectQuery obq1 = new ObjectQuery("Associators of {Win32_Directory.Name='D:'} Where ResultRole = PartComponent ");
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(ManagementScope1, obq1);
foreach (ManagementObject ManagementObject2 in searcher1.Get())
{
lvData[0] = ManagementObject2["FileName"].ToString();
lvData[1] = formatSize(Convert.ToInt64(ManagementObject2["FileSize"]));
ListViewItem lvItem = new ListViewItem(lvData, 0);
lvFiles.Items.Add(lvItem);
}
formatSize() as follows :
protected string formatSize(Int64 lSize)
{
//Format number to KB
string stringSize = "";
NumberFormatInfo myNfi = new NumberFormatInfo();
Int64 lKBSize = 0;
if (lSize < 1024)
{
if (lSize == 0)
{
//zero byte
stringSize = "0";
}
else
{
//less than 1K but not zero byte
stringSize = "1";
}
}
else
{
//convert to KB
lKBSize = lSize / 1024;
//format number with default format
stringSize = lKBSize.ToString("n", myNfi);
//remove decimal
stringSize = stringSize.Replace(".00", "");
}
return stringSize + " KB";
}
I also tried with this link, but i fail because of Object reference not set to an instance of an object error when i used as,
FolderSize += (UInt64)ManagementObject2["FileSize"];
lvData[1] = formatSize(Convert.ToInt64 (FolderSize));
So kindly someone help me to overcome this issue.