views:

88

answers:

4

I don't get the abstractions and the terminology :-(

For example, DirectoryInfo.FullName is defined as the full path of the directory or file, but it's a string! So is DirectoryInfo.Name, FileInfo.FullName, Path.GetDirectoyName and so on.

This means that in .Net there is no "depth" (or "meat" - my English isn't so good) for the file system objects. There's no protection from a type system. I can't, for example, define two Path objects and ask if one of them is "above" the other - I have to manipulate the strings. I can't differentiate between a Path that identifies a directory and a path that identifies a file. I can't do anything!-( Just manipulate strings.

Is this correct (or am I simply missing something). If correct, are there any alternatives?

+3  A: 

Take a look at System.IO.Path. To find out if you have a file, you can do Path.GetFileName("C:\\test\\") == String.Empty. Or if you are sure the file has an extension you can use Path.HasExtension.

Yuriy Faktorovich
Thanks, Yuriy, but there again: First, there is no Path object. And, it's not typesafe - everything is a string. Even the example they provide starts with: string path1 = @"c:\temp\MyTest.txt"; string path2 = @"c:\temp\MyTest"; string path3 = @"temp";But a path isn't a string - see my comment to scottm below.
Avi
+1  A: 

You should take a look at the Path class and the Uri class.

MSDN links:

Path Class

Uri Class

For example with the Path class you have methods to get only the directory, only the filenam or even only the filename without the extension for a given path.

With the Uri class you have a property to tell you if it's an absolute Uri (IsAbsoluteUri) and a method to make a relative Uri (MakeRelativeUri). Check the Uri FAQ for further information.


Example for traversing a path from top to bottom:

var dir = new DirectoryInfo(Environment.CurrentDirectory);
do
{
    Console.WriteLine(dir.Name);

    dir = dir.Parent;
} while (dir != null);
João Angelo
I feel better about the Uri - there not everything is a string, but for the file system?
Avi
@Avi, the `Uri` is useful when you want to make relative paths. Beside that, I stick with `Path`, `DirectoryInfo`, etc.
João Angelo
A: 

What is a file path anymore than a string? You can use the Path class to manipulate the strings. The DirectoryInfo class also has a Parent to get it's parent directory, and FileInfo has a Directory property to get it's current directory. What else do you need?

scottm
I think on the file system as a forest of directed trees with two types of nodes (directories which can have children, and files which can't) and one type of labeled edges. A Path is NOT a string. It's a traversal of the tree from one node to another! A path HAS-A corresponding string - the concatenation of all the labels along its edges. Thus, for example, I could define an operation on a path such as "GoingUp" or "GoingDown".
Avi
IMHO, the path itself is a string representation of the location of a file/directory in the node tree.
scottm
+1  A: 

I think the typesafe objects for FS entities are FileInfo and DirectoryInfo themselves.

wRAR
Re my comment to scottm, these are the types for the nodes on the graph, not for the "road from one node to another"
Avi