8

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?

Enumy
  • 841
  • 2
  • 8
  • 20
  • 6
    `File.Exists` and `Directory.Exists` – Metro Smurf Sep 22 '14 at 13:57
  • 2
    You want to check something that does not even exist? If you want to check if a given name is a valid filename you may simply check if it has an extension, if not it might be a directory. – MakePeaceGreatAgain Sep 22 '14 at 13:58
  • @MetroSmurf Seriously? " How to check whether the Path is a File or Directory if it doesn't exist?" – Rawling Sep 22 '14 at 13:58
  • Are you looking to determine if the path "string" is a directory or a file ? When it doesn't exists on the disk ? I don't think you can do that – Habib Sep 22 '14 at 13:58
  • Related? [Better way to check if Path is a File or a Directory?](http://stackoverflow.com/questions/1395205/better-way-to-check-if-path-is-a-file-or-a-directory) and [.NET How to check if path is a file and not a directory?](http://stackoverflow.com/questions/439447/net-how-to-check-if-path-is-a-file-and-not-a-directory) – Soner Gönül Sep 22 '14 at 13:59
  • 2
    Yes i want to treat the `Path` as a `String` and check if that `String` represents a `File` type of path or a `Directory` type of path – Enumy Sep 22 '14 at 13:59
  • 3
    @Hossam, A directory can have a name like `File1.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. `Directory1` is 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. – Habib Sep 22 '14 at 14:01
  • 2
    @MetroSmurf i can not use `File.Exists` or `Directory.Exists` if there's nothing actually to check at all – Enumy Sep 22 '14 at 14:01
  • @Habib Ys and that's exactly the point, not every `Path` ends with `/` so i need an alternative. – Enumy Sep 22 '14 at 14:05
  • 1
    @Hossam, that is an impossible situation, there is no alternative. – Habib Sep 22 '14 at 14:06
  • 1
    @Rawling - I assumed the OP's native language was not English and the question had a simple grammatical issue. – Metro Smurf Sep 22 '14 at 14:11
  • I believe so, but i have pointed out what i want clearly. – Enumy Sep 22 '14 at 14:15
  • @Hossam - could you please edit your question to match accepted answer? Because currently answer is wrong based on the question - it possibly works for your needs, but it is really hard to figure out what your needs are from your question. – Alexei Levenkov Sep 22 '14 at 14:40
  • 1
    A path that does not exist is neither a file nor a directory – 4thex Nov 12 '20 at 15:57

2 Answers2

7

If files in your scenario must have extensions then you could use this method.

NOTE: It is legal in windows to have periods in directories, but this was mostly introduced for cross operating system compatibility of files. In strictly windows environments it is considered bad form to have files without extensions or to put periods or spaces in directory names. If you do not need to account for that scenario then you could use this method. If not you would have to have some sort of flag sent through the chain or a structure to identify the intent of the string.

var ext = System.IO.Path.GetExtension(strPath);
if(ext == String.Empty)
{
    //Its a path
}

If you do not need to do any analysis on file type you can go as simple as:

if(System.IO.Path.HasExtension(strPath))
{
    //It is a file
}
Carter
  • 697
  • 5
  • 22
  • 3
    A little modification to be `Boolean Extention = System.IO.Path.HasExtension("//Path")` – Enumy Sep 22 '14 at 14:19
  • 3
    +0: Note: while this approach may work for OP it is wrong: there is no restrictions on folder names to not include dot. It is perfectly ok to have folder named "MyImages.jpg" or "Game.Saves". – Alexei Levenkov Sep 22 '14 at 14:38
  • 1
    @AlexeiLevenkov This was needed for the `Application` usage and not involving the `User`and as for, it is not possible for the a `Directory` to include an `Extension` signature – Enumy Sep 22 '14 at 15:13
  • @AlexeiLevenkov Your point is moot though. System.IO.Path.GetExtension is smart enough to start from the end of the string and identify if there is an extensions before the first directory separator. If your files must have extensions this method works fine. – Carter Sep 22 '14 at 15:14
  • 2
    this method will not work if a file have no extension – Ariex Aug 18 '15 at 05:45
  • This methode can not work if directory has . in it like `C:\Temp\Subdir\ProjectFolder.info` this will be indicated as file, even if it is an directory. – Tib Schott Mar 02 '23 at 12:54
1

The short answer is that there is no 100% way to distinguish a folder from a file by path alone. A file does not have to have a file extension, and a folder can have periods in its name (making it look like a file extension).

MarkD
  • 171
  • 1
  • 3