문제

폴더 크기를 매일 기록하는 이전 앱을 변환하고 있습니다. 레거시 앱은 Scripting.FilesyStemObject 라이브러리를 사용합니다.

Set fso = CreateObject("Scripting.FileSystemObject")
Set folderObject = fso.GetFolder(folder)
size = folderObject.Size

System.io.directory 및 System.io.directoryInfo 클래스에는 동등한 메커니즘이 없습니다.

.NET에서 동일한 결과를 얻으려면 실제로 전체 폴더 구조를 재귀 적으로 걸어 가야합니까?

업데이트 : @Jonathon/Ed- 감사합니다 .... 생각처럼. 스크립팅을 참조 할 것 같아요 .filesystemobject com 라이브러리. 내 앱의 .NET 순도를 깨뜨린 경우에도 마찬가지로 작동합니다. 내부보고 앱을위한 것이므로 그렇게 큰 문제는 아닙니다.

도움이 되었습니까?

해결책

나는 당신이 이미 대답을 알고 있다고 생각합니다. 디렉토리의 모든 파일을 추가해야합니다 (자식 디렉토리뿐만 아니라). 나는 이것에 대한 내장 기능을 모르지만, 모든 것을 알지 못합니다 (가까운 것은 아닙니다).

다른 팁

슬프게도, 그렇습니다 ... 누가 이유를 아는가.

public static long DirSize(DirectoryInfo d) 
{    
    long Size = 0;    
    // Add file sizes.
    FileInfo[] fis = d.GetFiles();
    foreach (FileInfo fi in fis) 
    {      
        Size += fi.Length;    
    }
    // Add subdirectory sizes.
    DirectoryInfo[] dis = d.GetDirectories();
    foreach (DirectoryInfo di in dis) 
    {
        Size += DirSize(di);   
    }
    return(Size);  
}

볼 수 있듯이 :

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

Mads Kristensen 게시 이것에 대해 잠시 동안 ...

private double size = 0;

private double GetDirectorySize(string directory)
{
    foreach (string dir in Directory.GetDirectories(directory))
    {
        GetDirectorySize(dir);
    }

    foreach (FileInfo file in new DirectoryInfo(directory).GetFiles())
    {
        size += file.Length;
    }

    return size;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top