Search code examples
vb.netfile-exists

My.Computer.FileSystem.FileExists doesnt work [vb.net]


I cant understand why i cant find files, its on right location. It was working before. But now it works only for certain files in the same location. Im 100% sure that the path and name of the file is the same as in the code.... Can't understand while i cant find the files...

 If ComboBox2.Text = "  1YZ-C01C 567.737.061 CA" Then
        If My.Computer.FileSystem.FileExists("source/Chr/1YZ-C01C 567.737.061-eeprom.txt") Then
            If My.Computer.FileSystem.FileExists("source/Chrysler/1YZ-C01C 567.737.061-erom.txt") Then
                Button2.Enabled = False
                button1.Enabled = False 
        Else
            ErrorOops.Show()
            Button2.Enabled = True
            Label1.Text = "Cant Find the file."
            Exit Sub
        End If
        Else
            ErrorOops.Show()
            Button2.Enabled = True
            Label1.Text = "Cant Find the file."
            Exit Sub
        End If
    End If

Solution

  • You need to supply the full path. ("source/Chr/1YZ-C01C 567.737.061-eeprom.txt") is not the full path.

    Use:

    Path.Combine(Path, File)
    

    So in your example File would be: "1YZ-C01C 567.737.061-eeprom.txt" and the full path (starting with C: D: or whatever) to "source/Chr"

    Typical Example:

    dim MyPath as string = "C:\MyData\source\Chr"
    dim MyPath as string = Application.StartupPath & "\source\Chr" 'Alternative
    dim MyFile as string = "1YZ-C01C 567.737.061-eeprom.txt"
    If My.Computer.FileSystem.FileExists(Path.Combine(MyPath,MyFile)) then
    ...
    end if