700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【千律】C++基础:删除只读属性文件 文件剪切 修改文件扩展名

【千律】C++基础:删除只读属性文件 文件剪切 修改文件扩展名

时间:2022-09-27 10:36:14

相关推荐

【千律】C++基础:删除只读属性文件 文件剪切 修改文件扩展名

1.删除带有只读属性的文件

#include <tchar.h>#include <windows.h>int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){// 变量初始化LPCTSTR ExistFile = _T("D:\\File.txt");// 获取文件属性DWORD FileAttribute = GetFileAttributes(ExistFile);// 判断文件属性if (FileAttribute == INVALID_FILE_ATTRIBUTES){// 隐藏属性 -- 直接返回return FALSE;}else if (FileAttribute & FILE_ATTRIBUTE_READONLY){// 只读属性 -- 修改文件属性SetFileAttributes(ExistFile, FILE_ATTRIBUTE_NORMAL);}// 文件的删除BOOL ResDelete = DeleteFile(ExistFile);if (!ResDelete){// 显示提示框MessageBox(NULL, _T("删除失败"), _T("提示窗口"), MB_OK);}return 0;}

2. 文件剪切功能的实现

#include <tchar.h>#include <windows.h>void FileCut(LPCTSTR ExistFile, LPCTSTR NewFile, BOOL Cover){// 文件的复制BOOL ResCopy = CopyFile(ExistFile, NewFile, Cover);// 文件的删除if (ResCopy){BOOL ResDelete = DeleteFile(ExistFile);}}int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){// 变量初始化LPCTSTR ExistFile = _T("D:\\File.txt");LPCTSTR NewFile = _T("D:\\NewFile\\File.txt");// 文件的剪切FileCut(ExistFile, NewFile, FALSE);return 0;}

3. 修改文件的类型扩展名

#include <tchar.h>#include <windows.h>int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){// 变量初始化LPCTSTR ExistFile = _T("D:\\File.txt");LPCTSTR NewFile = _T("D:\\File.bat");// 文件的删除BOOL ResMove = MoveFile(ExistFile, NewFile);if (!ResMove){// 显示提示框MessageBox(NULL, _T("扩展名修改失败"), _T("提示窗口"), MB_OK);}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。