用MFC控件复制hosts文件到,C:\Windows\System32\drivers\etc路劲,如果复制成功,就删除原路劲hosts,并且用中文注释下
时间: 2023-05-27 11:05:34 浏览: 102
以下是使用MFC控件复制hosts文件到C:\Windows\System32\drivers\etc路径的代码:
```
void CopyHostsFile()
{
CString sourceFilePath = _T("C:\\Windows\\System32\\drivers\\etc\\hosts");
CString destFilePath = _T("C:\\Windows\\System32\\drivers\\etc\\hosts_copy");
// 复制文件
if (CopyFile(sourceFilePath, destFilePath, FALSE))
{
// 删除原文件
if (DeleteFile(sourceFilePath))
{
// 成功删除原文件
CString message = _T("成功复制hosts文件到C:\\Windows\\System32\\drivers\\etc路径,并且删除了原文件。");
AfxMessageBox(message, MB_OK | MB_ICONINFORMATION);
}
else
{
// 删除原文件失败
CString message = _T("成功复制hosts文件到C:\\Windows\\System32\\drivers\\etc路径,但是删除原文件失败。");
AfxMessageBox(message, MB_OK | MB_ICONWARNING);
}
}
else
{
// 复制文件失败
CString message = _T("复制hosts文件到C:\\Windows\\System32\\drivers\\etc路径失败。");
AfxMessageBox(message, MB_OK | MB_ICONERROR);
}
}
```
其中,`sourceFilePath`为要复制的文件路径,`destFilePath`为复制后的文件路径。使用`CopyFile`函数进行文件复制,如果复制成功,则使用`DeleteFile`函数删除原文件。如果删除原文件成功,则弹出提示框,提示成功复制和删除。如果删除原文件失败,则弹出警告提示框。如果文件复制失败,则弹出错误提示框。
注释如下:
```
// 复制hosts文件到C:\Windows\System32\drivers\etc路径,并删除原文件
void CopyHostsFile()
{
// 原文件路径
CString sourceFilePath = _T("C:\\Windows\\System32\\drivers\\etc\\hosts");
// 复制后的文件路径
CString destFilePath = _T("C:\\Windows\\System32\\drivers\\etc\\hosts_copy");
// 复制文件
if (CopyFile(sourceFilePath, destFilePath, FALSE))
{
// 删除原文件
if (DeleteFile(sourceFilePath))
{
// 成功删除原文件
CString message = _T("成功复制hosts文件到C:\\Windows\\System32\\drivers\\etc路径,并且删除了原文件。");
AfxMessageBox(message, MB_OK | MB_ICONINFORMATION);
}
else
{
// 删除原文件失败
CString message = _T("成功复制hosts文件到C:\\Windows\\System32\\drivers\\etc路径,但是删除原文件失败。");
AfxMessageBox(message, MB_OK | MB_ICONWARNING);
}
}
else
{
// 复制文件失败
CString message = _T("复制hosts文件到C:\\Windows\\System32\\drivers\\etc路径失败。");
AfxMessageBox(message, MB_OK | MB_ICONERROR);
}
}
```
阅读全文
相关推荐


















