256

Is there anything built into System.IO.Path that gives me just the filepath?

For example, if I have a string

@"c:\webserver\public\myCompany\configs\promo.xml",

is there any BCL method that will give me

"c:\webserver\public\myCompany\configs\"?

Indy9000
  • 8,651
  • 2
  • 32
  • 37
CantSleepAgain
  • 3,743
  • 3
  • 21
  • 18
  • 6
    possible duplicate of [How do I get the directory from a file's full path?](http://stackoverflow.com/questions/674479/how-do-i-get-the-directory-from-a-files-full-path) – bluish Feb 06 '14 at 16:26
  • FWIW: I've "given up" on the Path's handling of "paths" and we use our own methods with better expectations and uniformity with UNC (try to use GetDirectoryName on a UNC path) and conventions (eg. trailing /). – user2864740 Feb 09 '18 at 03:59
  • Unless the file or directory exists, there is no way of knowing whether `promo.xml` designates a file or a directory by that same name. Which is probably why `Path.GetDirectoryName()` is implemented so simple and just truncates the last segment, or removes the trailing slash if there is one. – Abel Jan 31 '19 at 01:58

6 Answers6

289

Path.GetDirectoryName()... but you need to know that the path you are passing to it does contain a file name; it simply removes the final bit from the path, whether it is a file name or directory name (it actually has no idea which).

You could validate first by testing File.Exists() and/or Directory.Exists() on your path first to see if you need to call Path.GetDirectoryName

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • 3
    There's no need to call `File.Exists()`. Indeed, it's rather counter-productive in the case where your reason for finding the directory name is to create it if it doesn't already exist. – Jon Hanna Sep 30 '10 at 00:12
  • 3
    His example explicitly notes a path with a file name. If that is the pattern of the paths he is testing, and if those paths represent existing files, checking File.Exists() surely would be useful, would you not agree? Because the situation could be otherwise, of course, I was just suggesting he 'could' use the Exists methods on File and/or Directory; obviously, as appropriate for his situation. – Andrew Barber Sep 30 '10 at 00:17
  • Yes, a path with a file name. There's nothing in that to indicate a file exists, as file names come first. – Jon Hanna Sep 30 '10 at 02:55
  • 4
    As I said; it's an option and it may help depending on what is known about the path. Or it may not be necessary at all. But testing File.Exists() and Directory.Exists() on the same path is a quick and easy way to know if a path, which exists, is a file or directory. – Andrew Barber Sep 30 '10 at 04:19
  • 5
    as a quick reference, in redundancy with the question, and "obvious" treat, you need to include `System.IO` for this to work. – cregox Sep 11 '12 at 12:57
85
Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm")); 
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
explorer
  • 11,710
  • 5
  • 32
  • 39
52

Path.GetDirectoryName() returns the directory name, so for what you want (with the trailing reverse solidus character) you could call Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
27
    string fileAndPath = @"c:\webserver\public\myCompany\configs\promo.xml";

    string currentDirectory = Path.GetDirectoryName(fileAndPath);

    string fullPathOnly = Path.GetFullPath(currentDirectory);

currentDirectory: c:\webserver\public\myCompany\configs

fullPathOnly: c:\webserver\public\myCompany\configs

Demodave
  • 6,242
  • 6
  • 43
  • 58
Kobie Williams
  • 424
  • 6
  • 7
7

Use GetParent() as shown, works nicely. Add error checking as you need.

var fn = openFileDialogSapTable.FileName;
var currentPath = Path.GetFullPath( fn );
currentPath = Directory.GetParent(currentPath).FullName;
d219
  • 2,707
  • 5
  • 31
  • 36
kevinwaite
  • 613
  • 1
  • 8
  • 20
4

I used this and it works well:

string[] filePaths = Directory.GetFiles(Path.GetDirectoryName(dialog.FileName));

foreach (string file in filePaths)
{   
    if (comboBox1.SelectedItem.ToString() == "")
    {
        if (file.Contains("c"))
        {
            comboBox2.Items.Add(Path.GetFileName(file));
        }
    }
}
Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Karam
  • 97
  • 1
  • 1