Get full path without filename from path that includes filename
Asked Answered
C

7

263

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\"?

Comptroller answered 30/9, 2010 at 0:2 Comment(3)
possible duplicate of How do I get the directory from a file's full path?Physoclistous
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 /).Industrialist
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.Kirwan
R
298

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

Rivers answered 30/9, 2010 at 0:5 Comment(5)
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.Transeunt
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.Rivers
Yes, a path with a file name. There's nothing in that to indicate a file exists, as file names come first.Transeunt
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.Rivers
as a quick reference, in redundancy with the question, and "obvious" treat, you need to include System.IO for this to work.Zasuwa
C
86
Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm")); 
Complicity answered 30/9, 2010 at 0:10 Comment(0)
T
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.

Transeunt answered 30/9, 2010 at 0:10 Comment(0)
J
29
    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

Jollification answered 6/2, 2018 at 16:59 Comment(1)
just to be clear they the same so its OR right?Volleyball
R
8

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;
Rhatany answered 3/7, 2017 at 15:27 Comment(0)
S
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));
        }
    }
}
Sgraffito answered 1/3, 2013 at 10:18 Comment(0)
I
0

A file or directory may not always be on disk. better than File.Exists() and/or Directory.Exists() conditions, we can use:

/// <summary> Determine if given path is file </summary>
public static bool IsFile(string path) => !IsDirectory(path);
/// <summary> Determine if given path is directrory </summary>
public static bool IsDirectory(string path) {

    return String.IsNullOrEmpty(Path.GetExtension(path));
}
Intentional answered 11/2 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.