Jump to content

Como saber o tamanho ocupado por pastas.


programadorvb6
 Share

Recommended Posts

Olá boa tarde pessoal.

Adaptei uma rotina ao meu programa, que me informa qual o espaço ocupado por uma pasta (até aqui tudo bem), o problema surgiu quando necessitei de verificar as "pastas especiais"

Como posso resolver esta situação?

- Tenho acesso a pasta: MyPictures

Ex: MsgBox("Tamanho ocupado : " & ObjFuncoes.FormatBytes(ObjFuncoes.Tamanho_Pasta(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), True)))

- Não tenho acesso à pasta: ProgramFilesX86

Ex: MsgBox("Tamanho ocupado : " & ObjFuncoes.FormatBytes(ObjFuncoes.Tamanho_Pasta(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX8),True)))

Grato desde já pela vossa atenção.

Programadorvb6

Public Class Funcoes

''' <summary>
''' Formata automáticamente valores de bytes para outros tamanhos.
''' </summary>
''' <param name="dblBytes">Valor em bytes a receber</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function FormatBytes(ByVal dblBytes As Double) As String
	Const KILOBYTE As Double = 1024
	Const MEGABYTE As Double = KILOBYTE ^ 2 '  kilobyte elevado a 2
	Const GIGABYTE As Double = KILOBYTE ^ 3 '  kilobyte elevado a 3
	Const TERABYTE As Double = KILOBYTE ^ 4 '  kilobyte elevado a 4
	Const PETABYTE As Double = KILOBYTE ^ 5 '  kilobyte elevado a 5
	'Seleciona o formato mais apropriado
	Select Case dblBytes
		Case Is >= PETABYTE
			Return System.Math.Round(dblBytes / PETABYTE, 2) & " PB."
		Case Is >= TERABYTE
			Return System.Math.Round(dblBytes / TERABYTE, 2) & " TB."
		Case Is >= GIGABYTE
			Return System.Math.Round(dblBytes / GIGABYTE, 2) & " GB."
		Case Is >= MEGABYTE
			Return System.Math.Round(dblBytes / MEGABYTE, 2) & " MB."
		Case Is >= KILOBYTE
			Return System.Math.Round(dblBytes / KILOBYTE, 2) & " KB."
		Case Else
			Return dblBytes & " Bytes"
	End Select
End Function

''' <summary>
''' Informa o tamanho em bytes de uma pasta ou várias.
''' Caso o utilizador necessite de aceder a pastas especiais :
''' =>  Tamanho_Pasta(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), True)
''' </summary>
''' <param name="Indicar_Caminho">Indique a pasta a obter nº bytes.</param>
'''  <param name="Incluir_SubPastas">Validar a pesquisa estendida a subpastas.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Tamanho_Pasta(ByVal Indicar_Caminho As String, ByVal Incluir_SubPastas As Boolean) As Double
	Try
		Dim Tamanho_Total As Double = 0
		Dim diBase As New DirectoryInfo(Indicar_Caminho)
		Dim Arquivos() As FileInfo
		If Incluir_SubPastas Then
			Arquivos = diBase.GetFiles("*", SearchOption.AllDirectories)
		Else
			Arquivos = diBase.GetFiles("*", SearchOption.TopDirectoryOnly)
		End If
		Dim ie As IEnumerator = Arquivos.GetEnumerator
		While ie.MoveNext
			Tamanho_Total += DirectCast(ie.Current, FileInfo).Length
		End While
		Return Tamanho_Total
	Catch ex As Exception
		MsgBox("Erro: " & ex.Message)
		Return -1
	End Try
End Function
 End Class

'No Modulo:

Module DefGlobais
Public ObjFuncoes As New Funcoes
End Module

'No Form:

Public Class Form1
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
	MsgBox("Tamanho ocupado : " & ObjFuncoes.FormatBytes(ObjFuncoes.Tamanho_Pasta(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), True)))
	MsgBox("Tamanho ocupado : " & ObjFuncoes.FormatBytes(ObjFuncoes.Tamanho_Pasta(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX8),True)))
  End Sub
End Class
Edited by programadorvb6

______________________________________________________________________________

Que minha coragem seja maior que meu medo e que minha força seja tão grande quanto minha fé.
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.