如何获取文件夹的总大小? [英] How to get the total size of a folder?

查看:90
本文介绍了如何获取文件夹的总大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

g''day小组,


我需要一些能够提供文件夹总大小的代码。在

MSDN中我找到了一些代码,但我无法弄清楚如何使用这段代码

...


我是.NET的新手,我一直在使用VB6。任何人都可以指出

我正确的方向吗?


以下代码如何给我al /> $ b $的总文件大小b文件夹c:\ test ?我在哪里放这个代码?如何从这个类和功能中获得总额

大小?

谢谢,

Ed

--------------------


进口系统

进口System.IO

导入Microsoft.VisualBasic

''上面已经在代码中的正确位置


Public Class ShowDirSize


公共共享功能DirSize(ByVal d As DirectoryInfo)As

Long

Dim Size As Long = 0

''添加文件大小。

Dim fis As FileInfo()= d.GetFiles()

Dim fi As FileInfo

For Each fi in fis


尺寸+ = fi.Length

下一步fi

''添加子目录尺寸。

昏暗subs()As Boolean


''如果subs(counter)= True那么


Dim dis As DirectoryInfo()= d.GetDirectories( )

Dim di As DirectoryInfo

For Each di In

尺寸+ = DirSize(di)

下一页di

Re转大小


''结束如果


结束功能''DirSize


公共超载共享Sub Main(ByVal args()As String)

如果args.Length<> 1然后

Console.WriteLine(你必须在命令行提供一个目录

参数。)

Else
Dim d As New DirectoryInfo(args(0))

Console.WriteLine(" {0}及其

子目录的大小为{1 }字节。",d,DirSize(d))

结束如果

结束子''主要

结束类''ShowDirSize

g''day group,

I need some code that will give me the total size of a folder. In the
MSDN I''ve found some code, but I cannot figure out how to use this code
at all ...

I am quite new to .NET, I have been using VB6 always. Can anyone point
me in the right direction?

How will the following code give me the total size of al files in
folder "c:\test" ? Where do I put this code? And how do I get the total
size out of this class and function?
thanks,
Ed
--------------------

Imports System
Imports System.IO
Imports Microsoft.VisualBasic
'' the above is already at the right spot in the code

Public Class ShowDirSize

Public Shared Function DirSize(ByVal d As DirectoryInfo) As
Long
Dim Size As Long = 0
'' Add file sizes.
Dim fis As FileInfo() = d.GetFiles()
Dim fi As FileInfo
For Each fi In fis

Size += fi.Length
Next fi
'' Add subdirectory sizes.
Dim subs() As Boolean

''If subs(counter) = True Then

Dim dis As DirectoryInfo() = d.GetDirectories()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size

''End If

End Function ''DirSize

Public Overloads Shared Sub Main(ByVal args() As String)
If args.Length <> 1 Then
Console.WriteLine("You must provide a directory
argument at the command line.")
Else
Dim d As New DirectoryInfo(args(0))
Console.WriteLine("The size of {0} and its
subdirectories is {1} bytes.", d, DirSize(d))
End If
End Sub ''Main
End Class ''ShowDirSize

推荐答案

没关系,找到一些其他代码,更短,做这项工作:


私有函数DirSize(ByVal path As String)As Long

Dim sz As Long = 0

Dim d As DirectoryInfo = New DirectoryInfo(path)

''获取文件长度

Dim f As FileInfo

For each f in d.GetFiles()

sz + = f.Length

下一页

''递归到目录中

Dim dx作为DirectoryInfo

每个dx在d.GetDirectories中( )

sz + = DirSize(dx.FullName)

下一个

返回sz

结束功能

never mind, found some other code, much shorter, doing the job:

Private Function DirSize(ByVal path As String) As Long
Dim sz As Long = 0
Dim d As DirectoryInfo = New DirectoryInfo(path)
'' get file length
Dim f As FileInfo
For Each f In d.GetFiles()
sz += f.Length
Next
'' recurse into directories
Dim dx As DirectoryInfo
For Each dx In d.GetDirectories()
sz += DirSize(dx.FullName)
Next
Return sz
End Function


Grouped,


你可以更好地使用Integer作为VBNet中的索引器,它是32Bit

计算机中最优化的一个。


我希望这有帮助,


Cor
Grouped,

You can better use the Integer as indexer in VBNet, that it for the 32Bit
computer the most optimize one.

I hope this helps,

Cor


" grouped2000" < GR ********* @ gmail.com> schrieb:
"grouped2000" <gr*********@gmail.com> schrieb:
我需要一些能给我一个文件夹总大小的代码。
I need some code that will give me the total size of a folder.




这并不容易:


计算目录的大小不仅仅是添加文件大小

< URL:http://blogs.msdn.com/oldnewthing/archive/ 2004/12/28 / 336219.aspx>


-

MS Herfried K. Wagner

MVP< URL: http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/petition/>



That''s not as easy:

Computing the size of a directory is more than just adding file sizes
<URL:http://blogs.msdn.com/oldnewthing/archive/2004/12/28/336219.aspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


这篇关于如何获取文件夹的总大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆