如题目所示,如何判断某个路径是否存在?
解决方案 »
- 程序在运行,但却不能生成窗口.
- 请问怎么样实现发送鼠标右键消息给某一指定托盘图标,使其弹出右键菜单
- 有关VC++对话框,想把对话框做得大一些,其中的按扭也随之进行美观排列;
- 搭建短信平台发送短信息,要多少成本阿,具体应该怎么做啊
- 求助:谁知道怎样实现对.rar文件的操作呀
- 我写的一个COM组件,注册成功,但如果在COM中调用了一个dll之后再注册就报错,急!
- 关于取得RADIO的值。
- 怎样利用ADO向Access表中Insert记录????
- 当鼠标移桌面边界时,如何再跟踪鼠标移动距离?
- vc中设置全局内存区
- 小弟现学ATL,请问那位有潘爱民的<<深入解析ATL>>,我买!!!
- “龙软”英文名称征集活动
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission
#include <iostream.h>
#include "Shlwapi.h"void main( void )
{
// Valid file path name (file is there).
char buffer_1[ ] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;// Invalid file path name (file is not there).
char buffer_2[ ] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;// Return value from "PathFileExists".
int retval;// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}else
{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}}
方法一:
检查给定路径是否根目录:BOOL PathIsRoot(LPCTSTR pPath);
说明:Returns TRUE for paths such as “\”, “ X:\”, “\\ server\ share”, or “\\ server\”。Paths such as “..\path2” will return FALSE.
用这两个函数要先:#include <shlwapi.h>;
再把这个文件加入工程:shlwapi.lib
方法二:
GetFileAttributes检查文件是否存在,并且检查是否文件夹属性FILE_ATTRIBUTE_DIRECTORY
----------------------------------------------------------
DWORD = GetFileAttributes(_T("f:\\win98"));
if(dwAttr != 0xFFFFFFFF && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
cout<<"exist"<<endl;
else
cout<<"NOT exist"<<endl;
----------------------------------------------------------
方法三:
用下面第113条的_access函数同样可以
----------------------------------------------------------
if(_access("f:\\win98",0)!=-1)
cout<<"exist"<<endl;
else
cout<<"NOT exist"<<endl;
----------------------------------------------------------
方法四:
用PathFileExists函数,见MSDN介绍,需要的条件同方法一。
另外,Shlwapi.h及Shlwapi.lib好象是安装PlatForm SDK后才有的文件。
#include <shlwapi.h> // 找不到建议你重装VC
#pragma comment(lib, "shlwapi.lib")