使用vb.net获取相应文件夹下相应文件的个数并显示

1. 前言

因为我们实验室是做机器视觉方向的, 经常需要拍图, 最近在做双相机的实验, 但是拍图的时候, 可能会有很小几率的丢帧现象, 就是一个相机拍到图了, 另一个相机没有, 这个很讨厌。
我们本来是通过观察
使用vb.net获取相应文件夹下相应文件的个数并显示_第1张图片
左下角的数字来确定文件夹中相应的图片的数量, 不过这个太费劲了, 并且数字也特别小, 于是我们就想写个小程序来处理这个问题。

2. 实现

这里我们使用vb.net 来实现, 为什么用这个呢, 其实自己也不清楚, 反正, 就是不想用MFC了, MFC 太冗余了。

2.1 需求分析

需求可以简单的归结为统计一个文件夹中相应文件的数目, 需要一个输入路径的edit box 以及设置图片格式的combobox 和 用来显示的label。 同时需要支持拖拽功能

2.2 代码实现

Public Class Form1
    Dim FileAttribL As FileAttribute
    Dim intFileCountL As Integer
    Dim strFileNameL As String
    Dim FileAttribR As FileAttribute
    Dim intFileCountR As Integer
    Dim strFileNameR As String
    Dim count As Integer
    Dim fileFormatSelect As String

    ' 获取左右相机的图片路径 , 统计相应的文件数目, 核心 DIR 函数调用
    Private Sub getFileNum()
        If Not LeftCameraPath.Text = "" Then
            strFileNameL = Dir(LeftCameraPath.Text.ToString + "\\" + fileFormatSelect, FileAttribL)
            Do While Not strFileNameL = ""
                intFileCountL = intFileCountL + 1
                strFileNameL = Dir()
            Loop
            LeftNum.Text = intFileCountL.ToString
            intFileCountL = 0
        End If

        If Not RightCameraPath.Text = "" Then
            strFileNameR = Dir(RightCameraPath.Text.ToString + "\\" + fileFormatSelect, FileAttribR)
            Do While Not strFileNameR = ""
                intFileCountR = intFileCountR + 1
                strFileNameR = Dir()
            Loop
            RightNum.Text = intFileCountR.ToString
            intFileCountR = 0
        End If
    End Sub

    ' 计时器使用
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        getFileNum()
    End Sub

    ' 初始化
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FileAttribL = FileAttribute.Normal Or FileAttribute.Directory Or FileAttribute.Hidden
        FileAttribR = FileAttribL
        intFileCountL = 0
        intFileCountR = 0
        Timer1.Interval = 500
        Timer1.Enabled = True
        fileFormat.SelectedIndex = 2
    End Sub

    ' 设置图片格式
    Private Sub fileFormat_SelectedIndexChanged(sender As Object, e As EventArgs) Handles fileFormat.SelectedIndexChanged
        fileFormatSelect = fileFormat.SelectedItem
    End Sub

    ' 支持拖拽功能
    Private Sub LeftCameraPath_DragDrop(sender As Object, e As DragEventArgs) Handles LeftCameraPath.DragDrop
        Dim MyFiles() As String
        MyFiles = e.Data.GetData(DataFormats.FileDrop)
        LeftCameraPath.Text = MyFiles(0)
    End Sub

    Private Sub LeftCameraPath_DragEnter(sender As Object, e As DragEventArgs) Handles LeftCameraPath.DragEnter
        e.Effect = DragDropEffects.All
    End Sub

    Private Sub RightCameraPath_DragDrop(sender As Object, e As DragEventArgs) Handles RightCameraPath.DragDrop
        Dim MyFiles() As String
        MyFiles = e.Data.GetData(DataFormats.FileDrop)
        RightCameraPath.Text = MyFiles(0)
    End Sub

    Private Sub RightCameraPath_DragEnter(sender As Object, e As DragEventArgs) Handles RightCameraPath.DragEnter
        e.Effect = DragDropEffects.All
    End Sub
End Class

2.3 实现效果

使用vb.net获取相应文件夹下相应文件的个数并显示_第2张图片

你可能感兴趣的:(VB.NET)