如何检查子文件夹文本文件是否存在

时间:2015-05-01 16:40:30

标签: vb.net text-files

我正在尝试搜索子文件夹中是否存在文本文件。

这是我正在使用的代码:

'Checks the program's root folder to see if the root folder exists.
Dim FolderName = New DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Cake Orders\" & TextBox1.Text))
Dim McKnwoll As String = Path.Combine(FolderName.FullName, Trim(TextBox2.Text) & (" ") & Trim(TextBox3.Text) + ".RTO")
If Not McKnwoll.Exists Then
    ‘Message to user that file does not exist in sub-folder
Else
     ‘Message to user that file does exists in sub-folder
End If

我收到的错误是“存在'不是' String'的成员。如何重新处理我的代码以检查名称格式为" TextBox2.Text&的文本文件。 ("")& TextBox3.Text +" .RTO&#34 ;;存在。

我正在使用Visual Basic 2010 Express。谢谢。

2 个答案:

答案 0 :(得分:0)

File.Exists返回一个布尔值,指示某个路径上的文件是否存在:

If File.Exists(pathToFile) Then
    ...
End If

请务必在源代码文件的顶部添加Imports System.IO

答案 1 :(得分:0)

你在编程方面看起来很新。欢迎。

您收到的错误消息('存在'不是'字符串'的成员)会直接告诉您错误:您尝试询问字符串(某些文本)是否存在存在,但你想做的是询问文件是否存在。

提供有关文件信息的类称为" FileInfo",而FileInfo具有" Exists"您可以致电的财产:

Dim myFileInfo As New FileInfo(McKnwoll)
If myFileInfo.Exists Then
  'do something
End If

这是面向对象的答案,Heinzi更注重服务,但当然也有效。

我注意到您的代码中存在其他几个小问题,例如:

"Cake Orders\" & TextBox1.Text

不使用Path.Combine,但使用fix a" \"进行字符串连接。作为目录分隔符。或者这里没有真正使用DirectoryInfo,文件夹中的字符串就足够了。

您还尝试在一个代码块中处理3个不同的问题(从用户界面读取值,构造文件的全名,检查文件是否存在)。我会将它们分成3个不同的(实际上是4,我会添加另一个用于显示错误消息)。

你的简单几行代码就像这样复杂; - )

Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ReadFileIfPossible(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    End Sub

    Private Sub ReadFileIfPossible(subfolder As String, part1 As String, part2 As String)
        'Get the path to the RTO file
        Dim myFilePath As String = Nothing
        Try
            myFilePath = GetRtoFilePath(subfolder, part1, part2)
        Catch ex As Exception
            DisplayErrorMessage("Error constructing file name! Please check the values of TextBox1, TextBox2 and TextBox3.")
            Return
        End Try
        'Get the file info
        Dim myFile As FileInfo = Nothing
        Try
            myFile = New FileInfo(myFilePath)
        Catch ex As Exception
            DisplayErrorMessage(ex.Message)
            Return
        End Try
        'Check whether it exists
        Dim myExists As Boolean = False
        Try
            myExists = myFile.Exists 'it's IO, everything might throw an exception...
        Catch ex As Exception
            DisplayErrorMessage(ex.Message)
            Return
        End Try
        'Display message if not found
        If (Not myExists) Then
            DisplayErrorMessage("File ""{0}"" could not be found!", myFilePath)
            Return
        End If
        'Read the file
        Dim myLines As String() = Nothing
        Try
            myLines = File.ReadAllLines(myFile.FullName, New UTF8Encoding(True))
        Catch ex As Exception
            DisplayErrorMessage(ex.Message)
            Return
        End Try
        'Do something with it...
    End Sub

    Private Shared Function GetRtoFilePath(subfolder As String, part1 As String, part2 As String) As String
        'Check args
        If (subfolder Is Nothing) Then Throw New ArgumentNullException("subfolder")
        If (part1 Is Nothing) Then Throw New ArgumentNullException("part1")
        If (part2 Is Nothing) Then Throw New ArgumentNullException("part2")
        'Normalize args
        part1 = part1.Trim()
        part2 = part2.Trim()
        'Build path
        Dim myDesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        Dim myFolderPath As String = Path.Combine(myDesktopPath, "Cake Orders")
        myFolderPath = Path.Combine(myFolderPath, subfolder)
        Dim myFileName As String = String.Format("{0} {1}.RTO", part1, part2)
        Dim myResult As String = Path.Combine(myFolderPath, myFileName)
        myResult = Path.GetFullPath(myResult)
        'Return result
        Return myResult
    End Function

    Private Sub DisplayErrorMessage(message As String, ParamArray args As Object())
        Dim myMsg As String = String.Format(message, CType(args, Object()))
        MsgBox(myMsg, MsgBoxStyle.OkOnly, "Error")
    End Sub

End Class

玩得开心。