从文件名获取目录名 [英] Getting a directory name from a filename

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

问题描述

我有一个文件名(C:\folder\foo.txt),我需要检索非托管C ++中的文件夹名称(C:\folder)。在C#中我会做这样的:

I have a filename (C:\folder\foo.txt) and I need to retrieve the folder name (C:\folder) in unmanaged C++. In C# I would do something like this:

string folder = new FileInfo("C:\folder\foo.txt").DirectoryName;

有没有可以在非托管C ++中使用的函数从文件名中提取路径? p>

Is there a function that can be used in unmanaged C++ to extract the path from the filename?

推荐答案

有一个标准的Windows功能, PathRemoveFileSpec 。如果您只支持Windows 8及更高版本,强烈建议您使用 PathCchRemoveFileSpec 改为。除了其他改进之外,它不再局限于 MAX_PATH (260)个字符。

There is a standard Windows function for this, PathRemoveFileSpec. If you only support Windows 8 and later, it is highly recommended to use PathCchRemoveFileSpec instead. Among other improvements, it is no longer limited to MAX_PATH (260) characters.

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

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