Bobski Bobski - 11 days ago 7
Vb.net Question

.GetFiles (path).length seems to think there are multiple files when there is only one

I'm trying to have my application recognize a file that I have in my local drive but it seems to think that there are multiple files in there when there is only one....

This is what I'm doing ....

Dim filePath As String = "c:\Importantfile\FileToCheck"
Dim FileNo As Integer = Directory.GetFiles(filePath).Length


I mean it seems like this is the correct synthax since it actually recognizes that there are files but is there a reason why it would think there is multiple files - I made sure there is only 1 file....

Answer

If you know your filename (or part of it) or extension you can try using

Dim Path As String = "c:\Importantfile\FileToCheck"
Dim Pattern As String = "*.pdf" 'This gives you all pdf files
'or
Dim Pattern As String = "MyFileStartName*" 
'This gives you all files having name starting with"MyFileStartName"
Dim FileNum As Integer = Directory.GetFiles(Path, Pattern).Count