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
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.System.IO for this to work. 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
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)); } } } Path.GetDirectoryName(dialog.FileName) part has to do with the question (and other answers have that solution already). The rest does not seem relevant. Combobox? Filtering for filenames with a "c"?
promo.xmldesignates a file or a directory by that same name. Which is probably whyPath.GetDirectoryName()is implemented so simple and just truncates the last segment, or removes the trailing slash if there is one.