文件和回收站操作
(
四
)
本文讲述一些文件的基本操作,例如:文件夹的创建和删除,删除文件到回收站中,清空回收站,搜索文件,查找文件是否存在等。
1.
创建一个对话框工程:
FolderAndFile
。
2.
添加一个按钮为创建文件夹,代码如下:
char
buf
[256];
::
GetCurrentDirectory
(256,
buf
);
m_FolderName
.
GetWindowText
(
name
);
strcat
(
buf
,
"//"
);
strcat
(
buf
,
name
);
if
(
CreateDirectory
(
buf
,
NULL
))
{
MessageBox
(
"
文件夹创建成功
!"
);
return
;
}
else
{
MessageBox
(
"
文件夹创建失败,已经存在
!"
);
return
;
}
添加一个按钮为删除文件夹,代码如下:
char
buf
[256];
::
GetCurrentDirectory
(256,
buf
);
m_FolderName
.
GetWindowText
(
name
);
strcat
(
buf
,
"//"
);
strcat
(
buf
,
name
);
if
(
RemoveDirectory
(
buf
))
{
MessageBox
(
"
文件夹删除成功
!"
);
return
;
}
else
{
MessageBox
(
"
文件夹删除失败,文件不存在
!"
);
return
;
}
其中:
m_FolderName
为输入要创建文件夹名称的文本框对应变量。
3.
将文件删除到回收站以及清空回收站
添加一个按钮为“浏览文件”,代码如下:
CFileDialog
file
(
true
,
NULL
,
NULL
,
OFN_HIDEREADONLY
|
OFN_HIDEREADONLY
,
"All Files(*.*)|*.*||"
,
AfxGetMainWnd
());
if
(
file
.
DoModal
() ==
IDOK
)
{
name
=
file
.
GetPathName
();
m_BrowseFile
.
SetWindowText
(
name
);
}
其中:
m_BrowseFile
为一个显示浏览到要删除文件的文本框对应变量。
添加一个按钮为“删除”,即删除一个文件到回收站,如下:
char
filename
[100] =
"/0"
;
strcpy
(
filename
,
name
);
strcat
(
filename
,
"/0"
);
SHFILEOPSTRUCT
shfile
;
shfile
.
hwnd
= 0;
shfile
.
wFunc
=
FO_DELETE
;
shfile
.
pFrom
=
filename
;
shfile
.
pTo
=
NULL
;
shfile
.
fFlags
=
FOF_ALLOWUNDO
;
shfile
.
hNameMappings
=
NULL
;
shfile
.
lpszProgressTitle
=
NULL
;
SHFileOperation
(&
shfile
);
其中:
CString
strfilename
;
CString
name
;
添加一个按钮为“清空回收站”,代码如下:
GetWindowLong
(
m_hWnd
,0);
SHEmptyRecycleBin
(
m_hWnd
,
NULL
,
SHERB_NOCONFIRMATION
||
SHERB_NOPROGRESSUI
||
SHERB_NOSOUND
);
MessageBox
(
"
回收站已清空
!"
);
4.
搜索文件,添加一个按钮为“浏览”,代码如下:
BROWSEINFO
bi
;
char
buffer
[
MAX_PATH
];
ZeroMemory
(
buffer
,
MAX_PATH
);
bi
.
hwndOwner
=
GetSafeHwnd
();
bi
.
pidlRoot
=
NULL
;
bi
.
pszDisplayName
=
buffer
;
bi
.
lpszTitle
=
"
选择一个文件夹
"
;
bi
.
ulFlags
=
BIF_EDITBOX
;
bi
.
lpfn
=
NULL
;
bi
.
lParam
= 0;
bi
.
iImage
= 0;
LPITEMIDLIST
pList
=
NULL
;
if
((
pList
=
SHBrowseForFolder
(&
bi
))!=
NULL
)
{
char
path
[
MAX_PATH
];
ZeroMemory
(
path
,
MAX_PATH
);
SHGetPathFromIDList
(
pList
,
path
);
GetDlgItem
(
IDC_BROWSE_FILE
)->
SetWindowText
(
path
);
}
添加一个按钮为“查找”,如下:
CString
strpath
;
GetDlgItem
(
IDC_FILE_NAME_EDIT
)->
GetWindowText
(
strfilename
);
GetDlgItem
(
IDC_BROWSE_FILE
)->
GetWindowText
(
strpath
);
FindFile
(
strpath
);
bstop
=
FALSE
;
添加一个函数,如下:
void
CFolderAndFileDlg
::
FindFile
(
CString
strPath
)
{
CString
strTemp
;
if
(
strPath
.
Right
(1)!=
"//"
)
strTemp
.
Format
(
"%s//*.*"
,
strPath
);
else
strTemp
.
Format
(
"%s*.*"
,
strPath
);
CFileFind
findfile
;
BOOL
bfind
=
findfile
.
FindFile
(
strPath
);
while
(
bfind
)
{
bfind
=
findfile
.
FindNextFile
();
if
(
strfilename
==
findfile
.
GetFileName
())
{
m_filelist
.
AddString
(
findfile
.
GetFilePath
());
}
if
(
findfile
.
IsDirectory
()&&!
findfile
.
IsDots
())
{
FindFile
(
findfile
.
GetFilePath
());
}
if
(
bstop
)
return
;
}
}
其中:
m_filelist
为一个显示文件的列表控件对应变量。
5.
判断一个文件是否存在,添加一个按钮“是否存在”,代码如下:
UpdateData
(
TRUE
);
int
i
;
if
((
i
=
open
(
m_fileExit
,
_O_RDONLY
)) < 0)
{
MessageBox
(
"
文件不存在
!"
);
}
else
{
MessageBox
(
"
文件已经存在
!"
);
return
;
}
_lclose
(
i
);
其中:
m_fileExit
为输入的要判断的文件的文本框控件对应变量。
要包含:
#include
"io.h"
#include
"fcntl.h"
下次再接着讲一些文件的其他操作。