有时候我们需要判断某个指定的文件夹是否存在,如果不存在则提示用户是否创建
程序如下:
判断指定文件夹是否存在(保存在common文件中,方便其他程序调取)
' "文件夹存在CHECK"
Public Function isDirExist(ByVal strPath As String) As Boolean
Dim strDirTemp As String()
strDirTemp = strPath.Split("\")
strPath = String.Empty
For i As Integer = 0 To strDirTemp.Length - 1
' 判断数组内容.目的是防止输入的strPath内容如:c:\abc\123\ 最后一位也是"\"
If strDirTemp(i) <> "" Then
strPath += strDirTemp(i) & "\"
End If
Next
' 判断文件夹是否存在
isDirExist = System.IO.Directory.Exists(strPath)
End Function
画面程序调用:
' 检查设定的输出报表路径是否存在
If common.isDirExist(Me.txtOutputPath.Text.Trim()) = False Then
' 设定的输出报表路径不存在的场合,确认是否要创建该路径
If MsgBox("设定的输出报表路径: " & Me.txtOutputPath.Text.Trim() & " 不存在,是否创建该路径?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
' 创建设定的文件夹
My.Computer.FileSystem.CreateDirectory(Me.txtOutputPath.Text.Trim())
Return True
Else
Me.txtOutputPath.Focus()
Return False
End If
End If