从包含文件名的路径获取不包含文件名的完整路径

发布于 2024-09-25 23:13:56 字数 278 浏览 1 评论 0原文

System.IO.Path 中是否有任何内置内容只提供文件路径?

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

@“c:\webserver\public\myCompany\configs\promo.xml”,

是否有任何BCL方法可以给我

“c:\webserver\public\myCompany\configs\”?

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\"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

逆夏时光 2024-10-02 23:13:56

Path.GetDirectoryName()...但是您需要知道传递给它的路径确实包含文件名;它只是从路径中删除最后一位,无论是文件名还是目录名(它实际上不知道是哪一个)。

您可以首先在路径上测试 File.Exists() 和/或 Directory.Exists() 进行验证,看看是否需要调用 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).

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

烟燃烟灭 2024-10-02 23:13:56
Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm")); 
Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm")); 
清君侧 2024-10-02 23:13:56

Path.GetDirectoryName() 返回目录名称,因此对于您想要的内容(带有尾随反向斜线字符),您可以调用 Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar

Path.GetDirectoryName() returns the directory name, so for what you want (with the trailing reverse solidus character) you could call Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar.

猫腻 2024-10-02 23:13:56
    string fileAndPath = @"c:\webserver\public\myCompany\configs\promo.xml";

    string currentDirectory = Path.GetDirectoryName(fileAndPath);

    string fullPathOnly = Path.GetFullPath(currentDirectory);

当前目录:c:\webserver\public\myCompany\configs

fullPathOnly:c:\webserver\public\myCompany\configs

    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

把昨日还给我 2024-10-02 23:13:56

使用 GetParent() 如图所示,效果很好。 根据需要添加错误检查。

var fn = openFileDialogSapTable.FileName;
var currentPath = Path.GetFullPath( fn );
currentPath = Directory.GetParent(currentPath).FullName;

Use GetParent() as shown, works nicely. Add error checking as you need.

var fn = openFileDialogSapTable.FileName;
var currentPath = Path.GetFullPath( fn );
currentPath = Directory.GetParent(currentPath).FullName;
紫罗兰の梦幻 2024-10-02 23:13:56

我用过这个并且效果很好:

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));
        }
    }
}

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));
        }
    }
}
喜爱皱眉﹌ 2024-10-02 23:13:56

文件或目录可能并不总是位于磁盘上。比 File.Exists() 和/或 Directory.Exists() 条件更好,我们可以使用:

/// <summary> Determine if given path is file </summary>
public static bool IsFile(string path) => !IsDirectory(path);
/// <summary> Determine if given path is directrory </summary>
public static bool IsDirectory(string path) {

    return String.IsNullOrEmpty(Path.GetExtension(path));
}

A file or directory may not always be on disk. better than File.Exists() and/or Directory.Exists() conditions, we can use:

/// <summary> Determine if given path is file </summary>
public static bool IsFile(string path) => !IsDirectory(path);
/// <summary> Determine if given path is directrory </summary>
public static bool IsDirectory(string path) {

    return String.IsNullOrEmpty(Path.GetExtension(path));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文