The best way to test to see if a path is a file or directory

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
I am using Directory.Exists to test if a path is a dir or a file. I am sure it is one of the worst ways to do this.

Does anyone know of a better way to see if a path is a file or directory?
 
See, the problem with doing it that way is that if the directory does not exist then things will get messed up because I am assuming that it is a file. I know I can use that and still work around it but I think there should be a better way.

I saw someone use bytes to determine if a path was a file or directory but I dont know if that is the best way either because if the file is empty then it will be 0 bytes just like the directory.

But I really dont know how they used bytes to determine that, so I may be completely wrong. I would think that a folder has some attribute that no file has and that is what should be tested for. But I dont know. Please help.
 
hmm?

Check if a directory exists with Directory.Exists then if true use File.Exists to see if the file exists, else create the directory, then create the file.
 
Yes, that is exactly what I was talking about when I said,
"I know I can use that and still work around it but I think there should be a better way."
Maybe that is the best way. I dont know.
 
I know there was a DLL you could link to in vb6 (IMGHLP.DLL i think) that had a fuction that would create a path if it didnt exist, how ever many subdirs, do you want this?
 
Two lines of code is one of the worst ways to do something? :p

I think its pretty convenient considering that in VB6, before FileSystemObject, you had to try and open a file and trap for an error just to see if the file existed... :)

-Nerseus
 
I thought there was an issue with Dir$ but I cant think of it offhand... (maybe not detecting system files or something similar?)

Glad you know about Dir$ - its Awesome! Too bad it didnt include the ability to search through subdirectories recursively :)

-Nerseus
 
Back
Top