VB.NET 判断文件夹目录与文件是否存在检查

本博客详细介绍了在程序中如何通过自定义函数来检查文件夹和文件是否存在,确保程序运行的前提条件得到满足。

Private Sub BTN_LOAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesBTN_LOAD.Click
    '文件夹 CHECK
    If Not blnDirExist(TXT_PATH.Text.Trim) Then
        MsgBox("文件路径不存在")
        BTN_LOAD.Focus()
        Exit Sub
    End If
    '文件 CHECK
    If Not blnFileExist(TXT_PATH.Text.Trim) Then
        MsgBox("文件不存在")
        BTN_LOAD.Focus()
        Exit Sub
    End If
End Sub

#Region "文件夹存在CHECK"
    Private Function blnDirExist (ByVal strPath As StringAs Boolean
        Dim strDirTemp As String()
        strDirTemp = strPath.Split("\")
        strPath = String.Empty
        For i As Integer = 0 To strDirTemp.Length – 2
            strPath += strDirTemp(i) & "\"
        Next

        blnDirExist = System.IO.Directory.Exists(strPath)
    End Function
#End Region

#Region "文件存在CHECK"
    Private Function blnFileExist (ByVal strFile As StringAs Boolean
        blnFileExist = System.IO.File.Exists(strFile)
    End Function
#End Region

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值