且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取完整路径文件名不从路径,包括文件名

更新时间:2022-10-15 11:20:21

Path.GetDirectoryName() ...但你要知道你的路径被传递给它包含文件名;它只是从路径中的最后一位,无论是文件名或目录名(它实际上不知道哪些)。



您可以通过测试首先确认 File.Exists()和/或 Directory.Exists()您的路径上先来看看,如果你需要调用 Path.GetDirectoryName


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