I have a function that automatically creates a specified Path by determining whether the String Path is a File or Directory.
Normally, i would use this if the path already exists:
FileAttributes attributes = File.GetAttributes("//Path");
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
Directory.CreateDirectory("//Path");
}
But what if it doesn't? How to check whether the String Path is a File or Directory if it doesn't exist?
File.ExistsandDirectory.ExistsPathas aStringand check if thatStringrepresents aFiletype of path or aDirectorytype of pathFile1.xml, there is no way for you to check if the string is a directory or a file. You need to rethink what you are trying to do. You may treat strings without an extension as a directory. But there is no such restriction at OS side. Similarly OS would allow a file name without any extension, e.g.Directory1is a valid file name. one way to check it would be if your path ends with/that means that the path explicitly point to a directory. But again, you can have paths without/at end and it can still point to a directory.File.ExistsorDirectory.Existsif there's nothing actually to check at all