對以下代碼進行了哪些修改,來自:
https://www.devcurry.com/2010/07/calculate-size-of-folderdirectory-using.html
是否還需要回傳檔案夾和子檔案夾中所有檔案的計數?
是否可以同時進行以節省時間?
C#
using System;
using System.Linq;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo dInfo = new DirectoryInfo(@"C:/Articles");
// set bool parameter to false if you
// do not want to include subdirectories.
long sizeOfDir = DirectorySize(dInfo, true);
Console.WriteLine("Directory size in Bytes : "
"{0:N0} Bytes", sizeOfDir);
Console.WriteLine("Directory size in KB : "
"{0:N2} KB", ((double)sizeOfDir) / 1024);
Console.WriteLine("Directory size in MB : "
"{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));
Console.ReadLine();
}
static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
{
// Enumerate all the files
long totalSize = dInfo.EnumerateFiles()
.Sum(file => file.Length);
// If Subdirectories are to be included
if (includeSubDir)
{
// Enumerate all sub-directories
totalSize = dInfo.EnumerateDirectories()
.Sum(dir => DirectorySize(dir, true));
}
return totalSize;
}
}
}
VB
Imports System
Imports System.Linq
Imports System.IO
Module Module1
Sub Main()
Dim dInfo As New DirectoryInfo("C:/Articles")
' set bool parameter to false if you
' do not want to include subdirectories.
Dim sizeOfDir As Long = DirectorySize(dInfo, True)
Console.WriteLine("Directory size in Bytes : " & _
"{0:N0} Bytes", sizeOfDir)
Console.WriteLine("Directory size in KB : " & _
"{0:N2} KB", (CDbl(sizeOfDir)) / 1024)
Console.WriteLine("Directory size in MB : " & _
"{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))
Console.ReadLine()
End Sub
Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
ByVal includeSubDir As Boolean) As Long
' Enumerate all the files
Dim totalSize As Long = dInfo.EnumerateFiles() _
.Sum(Function(file) file.Length)
' If Subdirectories are to be included
If includeSubDir Then
' Enumerate all sub-directories
totalSize = dInfo.EnumerateDirectories() _
.Sum(Function(dir) DirectorySize(dir, True))
End If
Return totalSize
End Function
End Module
我還假設它可以在一個執行緒中執行,并將結果存盤在資料庫中,以防它需要很長時間。
更新 1:
從檔案夾中的檔案計數中,我看到了一個答案(盡管不是被接受的)來獲取檔案計數使用 LINQ,如上面的代碼:
var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
select file).Count();
I haven't a huge amount of experience with LINQ and so was wondering if the code should or could be combined so the LINQ returns two values (size and count) to save time? I could have a separate thread for each task but thought it could be more efficient if there only needed to be one pass per folder.
We would want it to be able to handle millions of sub folders with a .png and a .jpg file in each.
Our folders look like:
/images/3/4/2/4/7/6/5/4/3/4/3/2/abc-234345674243.png
uj5u.com熱心網友回復:
嘗試轉儲遞回,并嘗試轉儲 linq - 它很慢并且會占用大量記憶體。
嘗試這個:
Dim strFolder = "C:\Users\AlbertKallal\Desktop"
Dim MyDir As New DirectoryInfo(strFolder)
Dim MyFiles() As FileInfo = MyDir.GetFiles("*.*", SearchOption.AllDirectories)
For Each MyFile As FileInfo In MyFiles
tSize = MyFile.Length
Next
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/443716.html
上一篇:對于唯一名稱,獲取第一個值并從其余值pandas資料幀中減去
下一篇:asp.net:為什么我得到System.Collections.Generic.List`1[System.String]當我嘗試從asp.net網格視圖中的串列中列印值時?
