vc学习历程(6)--文件的操作
2007-08-23 23:47
399 查看
1、文件夹的创建和删除:
用到以下几个函数:
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
);
用于获取当前的文件路径;
BOOL CreateDirectory(
LPCTSTR lpPathName, // pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor //这个结构定义了目录的安全特性
);
用于创建文件夹;
BOOL RemoveDirectory(
LPCTSTR lpPathName // pointer to directory to remove
);
用于删除文件夹;
通过这三个函数来对文件进行处理,事例代码如下:
创建:
char buf[256];
GetCurrentDirectory(256,buf);
m_name.GetWindowText(name); //获得文件名;
strcat(buf,"//");
strcat(buf,name);
if(CreateDirectory(buf,NULL))
{
MessageBox("文件创建成功");
return ;
}
删除:
char buf[256];
GetCurrentDirectory(256,buf);
m_name.GetWindowText(name);
strcat(buf,"//");
strcat(buf,name);
if(RemoveDirectory(buf))
{
MessageBox("删除文件成功");
return ;
}
2、把文件给删除到回收站中,在这里调用了SHFileOperation,SHFileOperation是Window提供的对文件系统对
象进行删除,移动,复制等操作的API函数;
WINSHELLAPI int WINAPI SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
lpFileOp是一个指向SHFILEOPSTRUCT结构的指针,
SHFILEOPSTRUCT
SHFILEOPSTRUCT的结构如下:
typedef struct _SHFILEOPSTRUCT{
HWND hwnd; 这是拥有者窗口句柄;
UINT wFunc; 文件的操作功能,有FO_COPY,FO_DELETE,FO_MOVE,FO_RENAME
LPCSTR pFrom; 源文件
LPCSTR pTo; 目标文件
FILEOP_FLAGS fFlags; 文件控制标志
BOOL fAnyOperationsAborted; 用户是否中断操作
LPVOID hNameMappings; 指向一个SHNAMEMAPPING结构的指针
LPCSTR lpszProgressTitle; 进程标题
} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;
程序代码如下:
char fileName[100]="/0";
strcpy(fileName,strText);//获取源文件
strcat(fileName,"/0");
SHFILEOPSTRUCT shfile;
shfile.hwnd=0;
shfile.wFunc=FO_DELETE;
shfile.pFrom=fileName;
shfile.pTo=NULL;
shfile.fFlags=FOF_ALLOWUND0;
shfile.hNameMappings=NULL;
shfile.lpszProgressTitle=NULL;
SHFileOperation(&shfile); 执行语句;
3、打开文件夹的浏览框用到了CFileDialog 这个类;
CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD
dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd =
NULL );
Parameters
bOpenFileDialog
Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box.
lpszDefExt
The default filename extension. If the user does not include an extension in the Filename edit
box, the extension specified by lpszDefExt is automatically appended to the filename. If this
parameter is NULL, no file extension is appended.
lpszFileName
The initial filename that appears in the filename edit box. If NULL, no filename initially
appears.
dwFlags
A combination of one or more flags that allow you to customize the dialog box. For a description
of these flags, see theOPENFILENAME structure in the Win32 SDK documentation. If you modify the
m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default
behavior intact.
lpszFilter
A series of string pairs that specify filters you can apply to the file. If you specify file
filters, only selected files will appear in the Files list box. See the Remarks section for more
information on how to work with file filters.
pParentWnd
A pointer to the file dialog-box object’s parent or owner window.
代码事例:
CFileDialog file(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"All File(*.*)
|*.*|",AfxGetmainWnd());
{
if(file.DoModal()==IDOK)
{
strText=file.GetPathName(); 获取文件路径地址
m_edit.SetWindowText(strText);
}
}
4、回收站的清空:
这里用到SHEmptyRecycleBin函数;
MSDN中的定义:
SHSTDAPI SHEmptyRecycleBin(
HWND hwnd, 父窗口句柄
LPCTSTR pszRootPath, 设置删除哪个磁盘的回收站,如何设置字符为空则清空所有的回收站;
DWORD dwFlags 清空回收站的功能参数
);
Empties the recycle bin on the specified drive.
Returns S_OK if successful, or an OLE-defined error value otherwise.
hwnd
Handle to the parent window of any dialog boxes that might be displayed during the operation.
This parameter can be NULL.
pszRootPath
Address of a NULL-terminated string that contains the path of the root drive on which the recycle
bin is located. This parameter can contain the address of a string formatted with the drive,
folder, and subfolder names (c:/windows/system . . .). It can also contain an empty string or
NULL. If this value is an empty string or NULL, all recycle bins on all drives will be emptied.
dwFlags
One or more of the following values: SHERB_NOCONFIRMATION No dialog confirming the deletion of
the objects will be displayed.
SHERB_NOPROGRESSUI No dialog indicating the progress will be displayed.
SHERB_NOSOUND No sound will be played when the operation is complete.
5、GetWindowLong 函数详解:
函数功能描述:用这个函数能够获得指定窗口的信息
函数原型:
LONG GetWindowLong( HWND hWnd,int nIndex )
参数:
hWnd:指定窗口的句柄
nIndex:需要获得的信息的类型
值 功能
nIndex取值如下:
GWL_EXSTYLE 得到扩展的窗口风格
GWL_STYLE 得到窗口风格
GWL_WNDPROC 得到窗口回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用
GWL_HINSTANCE 得到应用程序运行实例的句柄
GWL_HWNDPARENT 得到父窗口的句柄
GWL_ID 得到窗口的标识符
GWL_USERDATA 得到和窗口相关联的32位的值(每一个窗口都有一个有意留给创建窗口的应用程序是用的32位
的值)
当hWnd标识一个对话框时可以使用下面的值
Value Action
DWL_DLGPROC 得到对话框回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用
DWL_MSGRESULT 得到对话框回调函数中消息处理过程的返回值
DWL_USER 得到额外的应用程序私有信息,如一些句柄和指针等
返回值:
成功时,返回一个请求的32位的值
失败时,返回0,可以使用GetLastError来取得错误信息
示例:
long nStyle = ::GetWindowLong(hWnd, GWL_STYLE); // hWnd是一个编辑框的句柄
if(nStyle & ES_PASSWORD)
{
AfxMessageBox("这是一个密码域");
}
用到以下几个函数:
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
);
用于获取当前的文件路径;
BOOL CreateDirectory(
LPCTSTR lpPathName, // pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor //这个结构定义了目录的安全特性
);
用于创建文件夹;
BOOL RemoveDirectory(
LPCTSTR lpPathName // pointer to directory to remove
);
用于删除文件夹;
通过这三个函数来对文件进行处理,事例代码如下:
创建:
char buf[256];
GetCurrentDirectory(256,buf);
m_name.GetWindowText(name); //获得文件名;
strcat(buf,"//");
strcat(buf,name);
if(CreateDirectory(buf,NULL))
{
MessageBox("文件创建成功");
return ;
}
删除:
char buf[256];
GetCurrentDirectory(256,buf);
m_name.GetWindowText(name);
strcat(buf,"//");
strcat(buf,name);
if(RemoveDirectory(buf))
{
MessageBox("删除文件成功");
return ;
}
2、把文件给删除到回收站中,在这里调用了SHFileOperation,SHFileOperation是Window提供的对文件系统对
象进行删除,移动,复制等操作的API函数;
WINSHELLAPI int WINAPI SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
lpFileOp是一个指向SHFILEOPSTRUCT结构的指针,
SHFILEOPSTRUCT
SHFILEOPSTRUCT的结构如下:
typedef struct _SHFILEOPSTRUCT{
HWND hwnd; 这是拥有者窗口句柄;
UINT wFunc; 文件的操作功能,有FO_COPY,FO_DELETE,FO_MOVE,FO_RENAME
LPCSTR pFrom; 源文件
LPCSTR pTo; 目标文件
FILEOP_FLAGS fFlags; 文件控制标志
BOOL fAnyOperationsAborted; 用户是否中断操作
LPVOID hNameMappings; 指向一个SHNAMEMAPPING结构的指针
LPCSTR lpszProgressTitle; 进程标题
} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;
程序代码如下:
char fileName[100]="/0";
strcpy(fileName,strText);//获取源文件
strcat(fileName,"/0");
SHFILEOPSTRUCT shfile;
shfile.hwnd=0;
shfile.wFunc=FO_DELETE;
shfile.pFrom=fileName;
shfile.pTo=NULL;
shfile.fFlags=FOF_ALLOWUND0;
shfile.hNameMappings=NULL;
shfile.lpszProgressTitle=NULL;
SHFileOperation(&shfile); 执行语句;
3、打开文件夹的浏览框用到了CFileDialog 这个类;
CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD
dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd =
NULL );
Parameters
bOpenFileDialog
Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box.
lpszDefExt
The default filename extension. If the user does not include an extension in the Filename edit
box, the extension specified by lpszDefExt is automatically appended to the filename. If this
parameter is NULL, no file extension is appended.
lpszFileName
The initial filename that appears in the filename edit box. If NULL, no filename initially
appears.
dwFlags
A combination of one or more flags that allow you to customize the dialog box. For a description
of these flags, see theOPENFILENAME structure in the Win32 SDK documentation. If you modify the
m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default
behavior intact.
lpszFilter
A series of string pairs that specify filters you can apply to the file. If you specify file
filters, only selected files will appear in the Files list box. See the Remarks section for more
information on how to work with file filters.
pParentWnd
A pointer to the file dialog-box object’s parent or owner window.
代码事例:
CFileDialog file(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"All File(*.*)
|*.*|",AfxGetmainWnd());
{
if(file.DoModal()==IDOK)
{
strText=file.GetPathName(); 获取文件路径地址
m_edit.SetWindowText(strText);
}
}
4、回收站的清空:
这里用到SHEmptyRecycleBin函数;
MSDN中的定义:
SHSTDAPI SHEmptyRecycleBin(
HWND hwnd, 父窗口句柄
LPCTSTR pszRootPath, 设置删除哪个磁盘的回收站,如何设置字符为空则清空所有的回收站;
DWORD dwFlags 清空回收站的功能参数
);
Empties the recycle bin on the specified drive.
Returns S_OK if successful, or an OLE-defined error value otherwise.
hwnd
Handle to the parent window of any dialog boxes that might be displayed during the operation.
This parameter can be NULL.
pszRootPath
Address of a NULL-terminated string that contains the path of the root drive on which the recycle
bin is located. This parameter can contain the address of a string formatted with the drive,
folder, and subfolder names (c:/windows/system . . .). It can also contain an empty string or
NULL. If this value is an empty string or NULL, all recycle bins on all drives will be emptied.
dwFlags
One or more of the following values: SHERB_NOCONFIRMATION No dialog confirming the deletion of
the objects will be displayed.
SHERB_NOPROGRESSUI No dialog indicating the progress will be displayed.
SHERB_NOSOUND No sound will be played when the operation is complete.
5、GetWindowLong 函数详解:
函数功能描述:用这个函数能够获得指定窗口的信息
函数原型:
LONG GetWindowLong( HWND hWnd,int nIndex )
参数:
hWnd:指定窗口的句柄
nIndex:需要获得的信息的类型
值 功能
nIndex取值如下:
GWL_EXSTYLE 得到扩展的窗口风格
GWL_STYLE 得到窗口风格
GWL_WNDPROC 得到窗口回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用
GWL_HINSTANCE 得到应用程序运行实例的句柄
GWL_HWNDPARENT 得到父窗口的句柄
GWL_ID 得到窗口的标识符
GWL_USERDATA 得到和窗口相关联的32位的值(每一个窗口都有一个有意留给创建窗口的应用程序是用的32位
的值)
当hWnd标识一个对话框时可以使用下面的值
Value Action
DWL_DLGPROC 得到对话框回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用
DWL_MSGRESULT 得到对话框回调函数中消息处理过程的返回值
DWL_USER 得到额外的应用程序私有信息,如一些句柄和指针等
返回值:
成功时,返回一个请求的32位的值
失败时,返回0,可以使用GetLastError来取得错误信息
示例:
long nStyle = ::GetWindowLong(hWnd, GWL_STYLE); // hWnd是一个编辑框的句柄
if(nStyle & ES_PASSWORD)
{
AfxMessageBox("这是一个密码域");
}
相关文章推荐
- 孙鑫VC学习笔记:第十二讲 (四) 用API函数、CFile类操作文件
- VC学习资料收集(8):用Visual C++操作INI文件
- VC++学习(12):文件操作
- 孙鑫VC学习笔记:第十二讲 用API函数、CFile类操作文件
- vc学习历程(7)--文件浏览对话框
- 孙鑫VC学习笔记:第十二讲 介绍常量指针、指针常量,文本、二进制文件
- Java学习笔记(二十二):Java 文件操作 :系统找不到指定的路径
- Servlet学习之九Servlet文件操作&网站计数器
- iOS学习Core Data文件操作中调用NSManagedObjectContext类实例save:方法导致程序崩溃
- Linux运维学习历程-第十天-文件查找工具
- iOS学习之iOS沙盒(sandbox)机制和文件操作之NSFileManager(三)
- VC中使用CFileDialog进行文件操作
- Windows 8学习笔记(二十一)--C++ 文件异步操作
- Nginx学习历程_配置文件说明
- 学习linux之用户-文件-权限操作
- BMP位图文件结构及VC操作
- ant文件学习操作
- php最新学习-----文件的操作
- C语言文件输入和输出操作的学习心得(一)
- GIT学习之路第三天 文件操作