获取完整路径文件名不从路径,包括文件名 [英] Get full path without filename from path that includes filename

查看:253
本文介绍了获取完整路径文件名不从路径,包括文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有内置 System.IO.Path 任何东西,让我仅仅是文件路径?



例如,如果我有一个字符串




@C:\webserver\ public\myCompany\configs\promo.xml




在那里,这将使我的BCL方法




C:\webserver\public\myCompany\configs\?



解决方案

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

这篇关于获取完整路径文件名不从路径,包括文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆