Perform instruction in vb according to the weight that is contained in a folder

1

Dear good afternoon, I hope someone can help me, I have a form which shows images in a PictureBox control that are in a certain path of my pc, and they are changed every so often as a SlideShow .. what I want to do with this and I can not think of how .. is to put in a Timer control with a While statement, or Do While, that is checking the total weight of the files in the folder, and that when it resizes, it will reload the code with the new images that have been added ... I attach my code:

Private Sub Panel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        con.ConnectionString = "Data Source=Server;Initial Catalog=panelinfo;User ID=sa; Password=123456;"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "SELECT ubicacion FROM fotos"
        Dim lrd As SqlDataReader = cmd.ExecuteReader()
        If lrd.Read() Then
            lblSlideRuta.Text = lrd("ubicacion")
        End If
        con.Close()

        If lblSlideRuta.Text <> "" Then

            Dim di As New IO.DirectoryInfo(lblSlideRuta.Text)
            ImageDir = di.GetFiles("*.jpg").Concat(di.GetFiles("*.bmp")).Concat(di.GetFiles("*.png")).Concat(di.GetFiles("*.gif")).ToArray
            Dim dra As IO.FileInfo
            frm = New Form
            frm.Name = "frm"
            FullSizePic = New PictureBox
            FullSizePic.Dock = DockStyle.Fill
            FullSizePic.BackColor = Color.Black
            FullSizePic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom

            PicBoxImagenes.Controls.Add(FullSizePic)
            AddHandler frm.KeyDown, AddressOf frm_keydown
            'frm.Show()
            TimerSlide.Enabled = True
            'EnterFullScreen(frm)
            GetnextImage()
        End If
    End Sub

Private Sub GetnextImage()           
        If Not ImageDir Is Nothing Then
            If ImageIndex < ImageDir.Length - 1 Then
                ImageIndex += 1
                FullSizePic.ImageLocation = ImageDir(ImageIndex).FullName
            ElseIf ImageIndex = ImageDir.Length - 1 Then
                ImageIndex = 0
                FullSizePic.ImageLocation = ImageDir(ImageIndex).FullName
            End If
        End If
    End Sub
    
asked by Francisco 30.08.2017 в 18:39
source

1 answer

1

to know the size of the folder:

Code:

Private Function BuscaDir(sDir As String)
Dim MisCarpetas As String
Dim a() As String, i As Integer
Static iCarpetas As Integer, iArchivos As Integer
' todos los archivos y carpetas, incluso los ocultos de sólo lectura y archivos de sitema
MisCarpetas = Dir(sDir, vbDirectory + vbHidden + vbReadOnly + vbSystem)
ReDim a(0)
Do While MisCarpetas <> ""
 If MisCarpetas <> "." And MisCarpetas <> ".." Then
 If (GetAttr(sDir & MisCarpetas) And vbDirectory) = vbDirectory Then
 ' es una carpeta, redimensionamos y la añadimos al array
 ReDim Preserve a(i)
 a(i) = MisCarpetas
 i = i + 1
 Else
 ' es un archivo, lo sumamos a la variable estática
 iArchivos = iArchivos + 1
 End If
 End If
 ' siguiente...
 MisCarpetas = Dir
Loop
' sumamos las carpetas
iCarpetas = iCarpetas + i
For i = 0 To UBound(a)
 ' hacemos una recursividad a la función con cada una de las carpetas guardadas en el array
 If a(i) <> "" Then
 BuscaDir sDir & a(i) & "\"
 End If
 DoEvents
Next i
' mostramos los resultados
Label1.Caption = "Carpetas: " & iCarpetas
Label2.Caption = "Archivos: " & iArchivos

and to call the function it will only have to pass the directory in question:

Code:

BuscaDir "c:\Archivos de programa\"

Then you have me show it in the unit that you want to convert to measure. and I'll position you how to know what size a file has in case you need it, and to know the size of a file there are several ways:

with FSO:

Code:

Private Sub Form_Load()
Dim Fso As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objFile = Fso.GetFile("C:\a.txt")
MsgBox "El Archivo " & objFile.Name & ",Tiene un Tamaño de " & objFile.Size & " bytes", vbInformation + vbOKOnly, "Tamaño de archivo"
Set objFile = Nothing
Set Fso = Nothing
End Sub

with Len:

Code:

Private Sub Form_Load()
MsgBox Len("c:\a.txt") & " bytes"
End Sub

Now, what you can do to implement it is that when loading or opening your window run the function to know how much the box weighs, and save it in a variable (PesaXBytes), after that in the Tick event of a timer you can do an if, something like that

    if(PesaXbytes == ejecutas la funcion de mdedir tamaño){
//sigue siendo el mismo tamañao no hace nada
}else{
//agregaria las nuevas fotos
}

Something like that occurs to me, I hope it serves you.

Greetings.

    
answered by 30.08.2017 в 18:57