从包含文件名的路径中获取没有文件名的完整路径 [英] Get full path without filename from path that includes filename
问题描述
System.IO.Path
中是否有任何内置的东西只给我文件路径?
Is there anything built into System.IO.Path
that gives me just the filepath?
例如,如果我有一个 string
For example, if I have a string
@"c:webserverpublicmyCompanyconfigspromo.xml",
@"c:webserverpublicmyCompanyconfigspromo.xml",
有什么BCL方法可以给我
is there any BCL method that will give me
"c:webserverpublicmyCompanyconfigs"?
"c:webserverpublicmyCompanyconfigs"?
推荐答案
Path.GetDirectoryName()
... 但你需要知道你传递给它的路径确实包含文件名;它只是从路径中删除最后一位,无论是文件名还是目录名(它实际上不知道是哪个).
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).
您可以首先通过在您的路径上测试 File.Exists()
和/或 Directory.Exists()
来验证是否需要调用 Path.GetDirectoryName
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
这篇关于从包含文件名的路径中获取没有文件名的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!