3

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

thanks

Chris S
  • 64,770
  • 52
  • 221
  • 239
  • can not use Exist(). the file or Dir already deleted.. but have to check the deleted path was file or Dir path. –  Sep 05 '09 at 10:01
  • 2
    If the file/directory does not exist, there really is no way to know what the path used to point to. Wael Dalloul's answer is the best one then, but it will only work if you know that directory paths are always ended with a directory seperator. – Jørn Schou-Rode Sep 05 '09 at 10:09
  • Going to update your question as my answer was technically correct – Chris S Sep 05 '09 at 10:27
  • 1
    Are you using the FileWatcher class to detect things which are deleted? – Mikael Svenson Sep 05 '09 at 12:43

6 Answers6

10

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
  • 37,718
  • 15
  • 88
  • 122
4

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
  • 21,178
  • 26
  • 94
  • 142
2
if (Path.HasExtension(fileFolderPath) && !string.IsNullOrEmpty(Path.GetFileName(fileFolderPath)))
{
    //path is for file
}

Directory.Exists and File.Exists are not option when path is not exists.

Morbia
  • 4,144
  • 3
  • 21
  • 13
2

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
  • 22,172
  • 11
  • 48
  • 57
  • 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 Sep 05 '09 at 10:06
1

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.

Community
  • 1
  • 1
Chris S
  • 64,770
  • 52
  • 221
  • 239
  • 1
    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 Sep 05 '09 at 10:28
  • Wow exactly the same answer as the top one, but -2. I'll update for a deleted path – Chris S Sep 05 '09 at 10:30
  • @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 Sep 05 '09 at 10:43
  • 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 Sep 05 '09 at 10:51
1

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
  • 64,563
  • 18
  • 145
  • 216