原帖地址:http://randy.host22.com/archives/34
需要头文件:
shlwapi.h
其中有一些函数功能存在重复,我在认为比较常用的的函数上加红。
BOOL PathFileExists(LPCTSTR
lpszPath)
功能:检查文件
/
路径是否存在
LPTSTR PathFindFileName(LPCTSTR
pPath)
功能:获得路径中的文件名
例如:
PathFileFileName( “c:\\Program Files\\File.txt” ) = “File.txt”
。
如果路径中不包含文件名,则返回值是最下面的目录名。
LPTSTR PathFindExtension(LPCTSTR
pPath)
功能:获取路径或文件名中的文件扩展名
例如:
PathFindExtension(
“File.txt” ) = “.txt”
LPTSTR
PathFindNextComponent(LPCTSTR pszPath)
例如:
PathFindNextComponent(
“c:\\Program Files\\Directory” ) = “Program Files\\
Directory”
PathFindNextComponent( “c:\\Program Files” ) = “Program
Files”
BOOL PathFindOnPath( IN OUT
LPTSTR pszFile , IN LPCTSTR * ppszOtherDirs)
功能:在指定的目录中寻找文件。
参数:
pszFile
要寻找的文件名,确保
pszFile
有足够的大小来容纳
MAX_PATH
个字符。如果能找到指定
的文件,该参数将返回文件的全路径。
ppszOtherDirs
该参数是首先要寻找的目录列表,它可以为
NULL
,如果为
NULL
,函数将在系统目录,当前目录和由
PATH
设定的目录中查找。
LPTSTR PathGetArgs(LPCTSTR
pszPath)
功能:从路径中分析参数
例如:
LPTSTR pszParam = PathGetArgs( “notepad.exe
c:\\temp.txt” )
pszParam
返回
“c:\\temp.txt”
int PathGetDriveNumber(LPCTSTR
lpsz)
功能:从路径中分析盘符
返回值:成功返回
0 –25
,代表(
A – Z
),失败返回
–1
BOOL PathIsContentType(LPCTSTR
pszPath,LPCTSTR pszContentType)
功能:检查文件是否为指定的
ContentType
例如:
PathIsContentType( “hello.txt” , “text/plain” )
返回TRUE
PathIsContentType( “hello.txt” , “image/gif” )
返回FALSE
BOOL PathIsDirectory(LPCTSTR
pszPath)
功能:检查路径是否为有效目录。注意路径不能包含文件名
BOOL PathIsFileSpec(LPCTSTR
lpszPath)
功能:检查路径中是否带有
‘:’
和
‘\’
分隔符
BOOL PathIsHTMLFile(LPCTSTR
pszFile)
功能:检查是否为HTML文件
BOOL PathIsPrefix(IN LPCTSTR
pszPrefix,IN LPCTSTR pszPath)
功能:检查路径是否包含指定前缀,前缀如:
”
盘符
:\\”
、
“..\\”
、
”.\\”
PathIsPrefix ( “c:\\” , “c:\\hello.txt” )
返回
TRUE
BOOL PathIsRelative(LPCTSTR
lpszPath)
功能:检查路径是否是相对路径
BOOL PathIsRoot(LPCTSTR pPath)
功能:检查路径是否是根目录
BOOL PathIsSameRoot(LPCTSTR
pszPath1,LPCTSTR pszPath2);
功能:比较两个路径是否在同一盘符
BOOL PathIsUNC(LPCTSTR pszPath
);
功能:判断路径是否是
UNC
格式
BOOL PathIsURL(IN LPCTSTR pszPath
);
功能:判断路径是否是
URL
BOOL
PathMatchSpec
( LPCTSTR pszFileParam,LPCTSTR
pszSpec)
功能:用带有通配符(
*
号和
?
号)的字符串去比较另一个字符串
例如:
PathMatchSpec
( “http://news.sina.com.cn” ,
“*sina.com*” )
返回
TRUE
PathMatchSpec
(
“c:\\hello.txt” , “*.txt” )
返回
TRUE
void PathRemoveArgs(LPTSTR
pszPath);
功能:删除路径中带有的参数
LPTSTR PathRemoveBackslash(LPTSTR
lpszPath);
功能:删除路径结尾多余的
’\’
符号
void PathRemoveBlanks(LPTSTR
lpszString);
功能:删除字符串头尾的空格
void PathRemoveExtension( LPTSTR
pszPath);
功能:删除路径后面的文件扩展名
BOOL PathRemoveFileSpec( LPTSTR
pszPath);
功能:删除路径后面的文件名和
’\’
符号。该函数可以分析出一个文件的路径。
例:
szPath = “c:\\windows\\system32\\nt.dll” ;
调用
PathRemoveFileSpec( szPath )
后,
szPath =
“c:\\windows\\system32”
BOOL PathRenameExtension( IN OUT
LPTSTR pszPath,LPCTSTR pszExt);
功能:替换路径后面的文件扩展名,如果
pszPath
后面不带扩展名,则新的扩展名会被添加上去。
pszPath pszPath
至少需要
MAX_PATH
个字符的空间
pszExt
新的扩展名,必需包含
.
号
void PathStripPath(LPTSTR pszPath
);
功能:从路径中分析出文件名
例:
PathStripPath( “c:\\test\\hello.txt” ) =>
hello.txt
BOOL PathStripToRoot( LPTSTR
szRoot );
功能:从路径中分析出盘符
void PathUnquoteSpaces( LPTSTR
lpsz );
功能:从带引号的路径中取出路径
例:
szPath = “\”c:\\Program Files\””
PathUnquoteSpaces( szPath ) => szPath =
“c:\\Program Files”
附另一路径相关函数,由
ImageHlp
提供
BOOL SearchTreeForFile(
IN LPSTR RootPath,
IN LPSTR InputPathName,
OUT LPSTR OutputPathBuffer
);
功能:从指定目录树中寻找文件