有没有内置的东西System.IO.Path
只给我文件路径?
例如,如果我有 string
@ "C:\网络服务器\ PUBLIC\myCompany的\ CONFIGS\promo.xml"
是否有任何BCL方法可以给我
"C:\网络服务器\ PUBLIC\myCompany的\ CONFIGS \"?
And*_*ber 224
Path.GetDirectoryName()
...但是您需要知道传递给它的路径确实包含文件名; 它只是从路径中删除最后一位,无论是文件名还是目录名(它实际上不知道哪个).
您可以先通过测试File.Exists()
和/或Directory.Exists()
在您的路径上验证是否需要打电话Path.GetDirectoryName
exp*_*rer 67
Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm"));
Run Code Online (Sandbox Code Playgroud)
Jon*_*nna 51
Path.GetDirectoryName()
返回目录名称,因此您可以调用所需的(使用尾部反向solidus字符)Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar
.
string fileAndPath = @"c:\webserver\public\myCompany\configs\promo.xml";
string currentDirectory = Path.GetDirectoryName(fileAndPath);
string fullPathOnly = Path.GetFullPath(currentDirectory);
Run Code Online (Sandbox Code Playgroud)
如图所示使用'GetParent()',效果很好.根据需要添加错误检查.
var fn = openFileDialogSapTable.FileName;
var currentPath = Path.GetFullPath( fn );
currentPath = Directory.GetParent(currentPath).FullName;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
210902 次 |
最近记录: |