2

The closest I get is using new FileInfo(path).FullPath, but as far as I know FileInfo is for files only, not directory.

See also my comments to Jon Skeet's answer here for context.

Community
  • 1
  • 1
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • 1
    Add some examples. It's not clear what you want. – H H Oct 25 '11 at 09:01
  • Indeed context may prove my answer to be not what your looking for – Paul C Oct 25 '11 at 09:03
  • 1
    @@LouisRhys - What exactly is your question? The linked question does not tell me what you are looking for. You should be using **FileSystemInfo** and determine if its a File or a Directory. You keep trying to find faults in the provided answers without explaining the reasons. – Security Hound Oct 25 '11 at 11:57

8 Answers8

4

The Path class also gives you a lot of nice methods and properties, e.g. GetFullPath(). See MSDN for all details.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
2

Path.GetFullPath()

chown
  • 51,908
  • 16
  • 134
  • 170
ShdNx
  • 3,172
  • 5
  • 40
  • 47
0

Use the DirectoryInfo class for paths to directories. Works much in the same matter as FileInfo.

Note that the property for the path is called FullName.

DirectoryInfo di = new DirectoryInfo(@"C:\Foo\Bar\");
string path = di.FullName;

If you want to determine whether a path is a file or a directory, you can use static methods from the Path class:

string path1 = @"C:\Foo\Bar.docx";
string path2 = @"C:\Foo\";

bool output1 = Path.HasExtension(path1); //Returns true
bool output2 = Path.HasExtension(path2); //Returns false

However, paths could also contain something that might resemble an extension, so you might want to use it in conjunction with some other checks, e.g. bool isFile = File.Exists(path);

Lars Kristensen
  • 1,410
  • 19
  • 29
  • what if it's a file, not directory? – Louis Rhys Oct 25 '11 at 09:06
  • You really need to distinguish between when you want the path for a directory or when you want the path for a file. As far as I know, there is no generic "Path" class, to represent either a file or a directory. – Lars Kristensen Oct 25 '11 at 09:09
0

You can use file.getdirectory to get this done.

phwd
  • 19,975
  • 5
  • 50
  • 78
Paul C
  • 4,687
  • 5
  • 39
  • 55
0

I think it's-

DirectoryInfo.FullName
rare
  • 134
  • 4
0

Try this:

String strYourFullPath = "";
IO.Path.GetDirectoryName(strYourFullPath)
  • It doesn't return the full path – Louis Rhys Oct 25 '11 at 09:06
  • It does. when strYourFullPath is a file, then it returns the directory where it file is in, when its strYourFullPath is a directory, its stays the same. If this is not what you are looking for, then what are you trying to achive? – Vincent Van Eijsden Oct 25 '11 at 10:46
0

Use the DirectoryInfo class which extends FileSystemInfo and will give the correct result for either files or directories.

        string path = @"c:\somefileOrDirectory";
        var directoryInfo = new DirectoryInfo(path);
        var fullPath = directoryInfo.FullName;
wal
  • 17,409
  • 8
  • 74
  • 109
0

According to msdn, FileSystemInfo.FullName gets the full path of the directory or file, and can be applied to a FileInfo.

FileInfo fi1 = new FileInfo(@"C:\someFile.txt");
Debug.WriteLine(fi1.FullName); // Will produce C:\someFile.txt
FileInfo fi2 = new FileInfo(@"C:\SomeDirectory\");
Debug.WriteLine(fi2.FullName); // Will produce C:\SomeDirectory\
Otiel
  • 18,404
  • 16
  • 78
  • 126