您的位置:首页 > 其它

windows客户端开发--文件以及文件夹相关操作(windows api)

2016-11-18 17:54 363 查看
1 重命名 

不再赘述,看看之前的博客吧: 

C++中修改文件夹名以及文件名

2 判断是文件还是文件夹是否存在
// 判断文件是否存在
BOOL IsFileExist(const CString& csFile)
{
DWORD dwAttrib = GetFileAttributes(csFile);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
// 判断文件夹是否存在
BOOL IsDirExist(const CString & csDir)
{
DWORD dwAttrib = GetFileAttributes(csDir);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
// 判断文件或文件夹是否存在
BOOL IsPathExist(const CString & csPath)
{
DWORD dwAttrib = GetFileAttributes(csPath);
return INVALID_FILE_ATTRIBUTES != dwAttrib;
}

// 变体
BOOL IsPathExist(const CString & csPath)
{
WIN32_FILE_ATTRIBUTE_DATA attrs = { 0 };
return 0 != GetFileAttributesEx(csPath, GetFileExInfoStandard, &attrs);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

3删除文件
if(!_access(source,0))
{

SetFileAttributes(source,0);
if(DeleteFile(source))//删除成功
{
cout<<source<<" 已成功删除."<<endl;
}
else//无法删除:文件只读或无权限执行删除
{
cout<<source<<" 无法删除:文件为只读属性或无删除权限."<<endl;
}
}
else//文件不存在
{
cout<<source<<" 不存在,无法删除."<<endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

4删除文件夹下所有的文件
BOOL CDlgData::DeleteDirectory(char *sDirName)
{
CFileFind tempFind;
char sTempFileFind[200] ;

sprintf(sTempFileFind,"%s\\*.*",sDirName);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();

if (!tempFind.IsDots())
{
char sFoundFileName[200];
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));

if (tempFind.IsDirectory())
{
char sTempDir[200];
sprintf(sTempDir,"%s\\%s",sDirName,sFoundFileName);
DeleteDirectory(sTempDir);
}
else
{
char sTempFileName[200];
sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName);
DeleteFile(sTempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(sDirName))
{
return FALSE;
}
return TRUE;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

5 拷贝文件
bool CopyFile(const std::wstring &from_path, const std::wstring &to_path)
{
if (from_path.size() >= MAX_PATH ||
to_path.size() >= MAX_PATH) {
return false;
}
return (::CopyFileW(from_path.c_str(), to_path.c_str(),
false) != 0);
}
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9

6 获取文件大小
int64_t GetFileSize(const PathString &filepath)
{
WIN32_FIND_DATAW file_data;
HANDLE file = FindFirstFileW(filepath.c_str(), &file_data);

if (file == INVALID_HANDLE_VALUE)
return -1;
LARGE_INTEGER li = { file_data.nFileSizeLow, file_data.nFileSizeHigh };
FindClose(file);
return li.QuadPart;
}
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11

7 获取文件夹下所有文件
void GetFilesInDirectory(std::vector<string> &out, const string &directory)
{

HANDLE dir;
WIN32_FIND_DATA file_data;

if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */

do {
const string file_name = file_data.cFileName;
const string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

if (file_name[0] == '.')
continue;

if (is_directory)
continue;

out.push_back(full_file_name);
} while (FindNextFile(dir, &file_data));

FindClose(dir);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vs2012 windows