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

本文标签: 文件属性文件扩展名基础