tags:

views:

657

answers:

6

How can I identify whether a deleted path was a file or a directory using C# .Net?

thanks

+4  A: 

If you use Directory.Exists(...) on a file it will return false. Likewise if you use File.Exists(...) on a directory it will return false

TimothyP
+8  A: 

Assuming that the file/directory actually exists, you can use the two static methods:

Both accept a single string argument and returns a boolean value if the file/directory exists.

Another case is when you have a path that does not refer to an existing file/directory in the file system - maybe it points to some kind of "virtual file/directory" in a database, or the path points to a file/directory that used to exists, but is now (possibly) deleted. In this case, you will have to define the distinction of "file like paths" and "directory like paths" yourself. I can think of two approaches:

  • A: Directory paths ends with a directory seperator (as suggested by Wael Dalloul)
  • B: File paths have an extension seperator in their last path token

Lets test the two approaches on a couple of sample input strings:

  • c:\windows\ is a directory in both cases
  • c:\windows is a directory only when using approach B
  • c:\windows\notepad.exe is a file in both cases
  • c:\windows\system32\drivers\etc\hosts is a file only in approach A

As pointed out by these examples, none of the two approaches are guaranteed to give the expected answers in all cases, unless you are able to control exactly how the paths are designed from the beginning.

Jørn Schou-Rode
i have only a string. it is Dir or file? can not use Exist()
Ashish
@Ashish: both of the static methods mentioned in my answer takes exactly one string as argument.
Jørn Schou-Rode
+2  A: 

use File.Exists() or Directory.Exists(). They will tell you if there is a file / directory with that name. You then know what it is by checking which one returns treu.

Botz3000
+2  A: 

also you can use: path.GetFileName

If the last character of path is a directory or volume separator character, this method returns Empty, so you can check the result if it's empty so it's a directory else it's a file.

Wael Dalloul
Maybe. In this interpretation the string "c:\windows" is a *file*, but when I type this in the start > run dialog I get an Explorer window showing the Windows *directory*.
Jørn Schou-Rode
+1  A: 

If you have an extension then you could guess it was a file, but that would be a guess because you can always name your folder "myfile.txt". Another guess as mentioned would be if it ended in a path separator.

File.Exists and Directory.Exists both use GetFileAttributesEx which look at the attributes of a file, but as it's no longer there you don't have that option. If it's in the recycle bin you may be able to find it there - this question has details.

Chris S
Why would you want to extend Path class with a IsFile method when you already have File.Exists that does exactly the same thing?
Mikael Sundberg
Wow exactly the same answer as the top one, but -2. I'll update for a deleted path
Chris S
@Mikael File.Exists tells you if the file exists not if it's a directory, but you're right it is ultimately the same thing but with slightly less semantic value
Chris S
But as I said Path is static so that's not an option. If it was I would prefer Path path = new Path(@"C:\windows"); path.IsFile() personally. `Uri` doesn't do that as can't use the file system.
Chris S
A: 

You can use

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetError = true)]
static extern int GetFileAttributes(string lpFileName);

bool IsDirectory(string path) {
     return GetFileAttributes(path) & 16 == 16;
}

However Directory.Exists and File.Exists as suggested in the other answers is just as good.

See here for more details.

Preet Sangha