Uma Calculadora para Bytes/Megabytes/Gigabytes

18/06/2017

0

Olá!
Neste Exemplo deixo aqui como fazer uma calculadora conversora em Bytes, Megabytes e Gigabytes. Mostro em dois detalhes:
1º Ver o tamanho de arquivos em megabytes e também o tamanho de pastas.

: Adicione:

7 Textboxs:

TextFilePath
TextSizeBytes
TextDividendo1
TextDividendo2
Text1024bytesDivisor
TextResultado1
TexTOTALsize

2 Buttons: para buscas as Pastas e/ou Arquivos
Pasta
Arquivo

1 Timer:(opcional) Ótimo para monitorar
pastas que estão em uso, como exemplo da pasta "Temp"
Aqui está o código:
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Diagnostics.Process
Imports System.Windows.Forms
Namespace MSI_NFORMATICA
    'CONTATOS:
    ' Facebook: https://www.facebook.com/marinnermsi/
    'WhatsApp: 81 98414 9530
    'email: marinnermsi@hotmail.com
    'CNPJ: 17.336.564/0001-00
End Namespace
Public Class F1Calc
    Dim opFiles As New OpenFileDialog
    Dim opFolder As New FolderBrowserDialog
    Private Property SizeType As Object

    Private Sub F1Calc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = ProductName & " - Versão: " & ProductVersion & " - " & CompanyName
    End Sub

    Private Sub btArquivo_Click(sender As Object, e As EventArgs) Handles btArquivo.Click
        Try
            lblError.ResetText()
            TextSizeBytes.Clear()
            TextDividendo1.Clear()
            TextDividendo2.Clear()
            TexTOTALsize.Clear()
            TexTOTALsize.Clear()
            lblTipFile.ResetText()
            Display1.Image = Nothing
            opFiles.Multiselect = True
            If opFiles.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim Calculo As Decimal
                Dim Tamanho As Long = Long.Parse(My.Computer.FileSystem.GetFileInfo(opFiles.FileName).Length)
                Dim TC As String = TextSizeBytes.Text
                TextSizeBytes.Text = (My.Computer.FileSystem.GetFileInfo(opFiles.FileName).Length)
                Dim Tipo As String = ""
                TextFilePath.Text = opFiles.SafeFileName
                Dim Somando As Integer = 0
                If Tamanho < 1024 Then
                    Calculo = Tamanho

                ElseIf Tamanho > 1024 AndAlso Tamanho < (1024 ^ 2) Then 'KB
                    Calculo = Math.Round((Tamanho / 1024), 2)
                    Tipo = " Kb"

                ElseIf Tamanho > (1024 ^ 2) AndAlso Tamanho < (1024 ^ 3) Then 'MB
                    Calculo = Math.Round((Tamanho / (1024 ^ 2)), 2)
                    Tipo = " Mb"

                ElseIf Tamanho > (1024 ^ 3) AndAlso Tamanho < (1024 ^ 4) Then 'GB
                    Calculo = Math.Round((Tamanho / (1024 ^ 3)), 2)
                    Tipo = " Gb"

                ElseIf Tamanho > (1024 ^ 4) Then 'TB
                    Calculo = Math.Round((Tamanho / (1024 ^ 4)), 2)
                    Tipo = " Tb"
                End If

                TexTOTALsize.Text = Calculo.ToString & lblTipoSize.Text & Tipo

                'verificando o tipo de arquivo:
                Dim extFile As String = Path.GetExtension(TextFilePath.Text)


                If extFile = ".jpg" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo de imagem)"

                ElseIf extFile = ".png" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo de imagem)"

                ElseIf extFile = ".jpeg" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo de imagem)"

                ElseIf extFile = ".bitmap" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo de imagem)"

                ElseIf extFile = ".pdf" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo em PDF)"

                ElseIf extFile = ".docx" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo do Word)"

                ElseIf extFile = ".ppt" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo do Power Point)"

                ElseIf extFile = ".txt" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo de Texto)"

                ElseIf extFile = ".html" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & "  (Arquivo da Web)"

                ElseIf extFile = ".dll" Then
                    Display1.ImageLocation = (opFiles.FileName)
                    lblTipFile.Text = extFile & " (Arquivo Extensão de Aplicativo (.NetComponent)"
                End If
            End If


        Catch ex As Exception

        End Try

    End Sub

    Private Sub btPasta_Click(sender As Object, e As EventArgs) Handles btPasta.Click
        Try
            lblError.ResetText()
            TextSizeBytes.Clear()
            TextDividendo1.Clear()
            TextDividendo2.Clear()
            TexTOTALsize.Clear()
            TexTOTALsize.Clear()
            lblTipFile.ResetText()

            Display1.Image = Nothing
            TimerSizeFolder.Enabled = False
            opFolder.ShowNewFolderButton = False
            opFolder.ShowDialog()
            TextFilePath.Text = opFolder.SelectedPath
            TexTOTALsize.Text = "Calculando..."
            TimerSizeFolder.Enabled = True
            lblError.Text = "Processo feito com sucesso!"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub TimerSizeFolder_Tick(sender As Object, e As EventArgs) Handles TimerSizeFolder.Tick
        'PEGANDO O VALOR EM MEGABYTES:==========================================================
        Try
            Dim FileSystem, Folder As Object
            FileSystem = CreateObject("Scripting.FileSystemObject")
            'Folder = FileSystem.GetFolder(TRASH)
            Folder = FileSystem.GetFolder(TextFilePath.Text)
            Dim TamanhoBytes As Long = Folder.size
            TextSizeBytes.Text = TamanhoBytes

            '====================PRIMEIRO CÁLCULO:=====================
            TextDividendo1.Text = TextSizeBytes.Text
            TextResultado1.Text = TextDividendo1.Text / Text1024bytesDivisor.Text

            '=====================SEGUNDO CÁLCULO:============================

            'PASSANDO VALOR DO PRIMEIRO RESULTADO PARA O SEGUNDO CÁLCULO: 
            TextDividendo2.Text = TextResultado1.Text

            'AGORA, VAMOS DIVIDIR!
            TexTOTALsize.Text = TextDividendo2.Text \\ Text1024bytesDivisor.Text

            'PASSANDO PARA O RESULTADO FINAL:(APENAS COPIAREMOS DO 'TextResultado2')
            ' TexTOTAL.Text = TextResultado2.Text
            lblError.Text = "Processo feito com sucesso!"
            If TexTOTALsize.TextLength >= 3 Then
                lblTipoSize.Text = "Gigabytes"
            ElseIf TexTOTALsize.TextLength < 4 Then
                lblTipoSize.Text = "Megabytes"
            End If
        Catch ex As Exception
            lblError.Text = "Erros encontrados ou permissão de leitura de arquivos e pastas não concedida!"
        End Try

    End Sub

    Private Sub Display1_Click(sender As Object, e As EventArgs) Handles Display1.Click

    End Sub

    Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs)

    End Sub
End Class
Marinner Software

Marinner Software

Responder

Que tal ter acesso a um e-book gratuito que vai te ajudar muito nesse momento decisivo?

Ver ebook

Recomendado pra quem ainda não iniciou o estudos.

Eu quero
Ver ebook

Recomendado para quem está passando por dificuldades nessa etapa inicial

Eu quero

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar