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;}