admin管理员组文章数量:1660126
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;
}
版权声明:本文标题:【千律】C++基础:删除只读属性文件、文件剪切、修改文件扩展名 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729845205a1215046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论