Get full path without filename from path that includes filename
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\"?
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
Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm"));
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
.