用vc设计系统援救程序
2010-04-03 20:35:51 来源:WEB开发网三.程序实现
1.在头文件中加入:
CString m_strWinDir(' ',_MAX_DIR);
2.在构造函数中加入:
GetWindowsDirectory(m_strWinDir.GetBuffer(0),_MAX_DIR);
m_strWinDir.ReleaseBuffer();
3.加入一个进展条,并用ClassWazid生成一个控制型变量m_progress.
4.加入两个函数:
CString CRescueSysDlg::getMyDir() //用来得到程序的当前目录.
{
TCHAR sFilename[_MAX_PATH];
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFname[_MAX_FNAME]; //不带扩展名
TCHAR sExt[_MAX_EXT]; //扩展名,前面有"."
GetModuleFileName(AfxGetInstanceHandle(), sFilename, _MAX_PATH);
_tsplitpath(sFilename, sDrive, sDir, sFname, sExt);
CString homeDir(CString(sDrive) + CString(sDir));
int nLen=homeDir.GetLength();
if (homeDir.GetAt(nLen-1) != _T(''))
homeDir+=_T("");
return homeDir;
}
CString CRescueSysDlg::toShortPath(CString m_inDir) //用来将长路径名转化为短路径名.
{
char strBuffer[_MAX_PATH];
GetShortPathName(m_inDir,strBuffer,_MAX_PATH);
CString m_toDir(strBuffer);
return m_toDir;
}
5.为"开始备份"按钮或菜单项生成一个响应函数:
void CRescueSysDlg::OnBeginBkup()
{
CInputName m_inNameDlg;
// CinputName是一个对话框类,用来将用户输入的备份名称保存在inNameDlg .m_strInputEdit中.
if(IDCANCEL==m_inNameDlg.DoModal())
return;
CFile f1;
CString m_allFileName[20];
int m_savedNum=0;
if(f1.Open(getMyDir()+"backedCab.mqy",CFile::modeRead))
//读出用户以前所有备份的名称.
{
CArchive ar1(&f1,CArchive::load);
ar1>>m_savedNum;
for(int i=0;i
ar1>>m_allFileName[i];
ar1.Close();
f1.Close();
}
CTime m_curTime=CTime::GetCurrentTime();
CString strInputAdded,strIsFileName;
strInputAdded.Format("_%d_%d_%d_%d_%d",m_curTime.GetYear(),m_curTime.GetMonth(),
m_curTime.GetDay(),m_curTime.GetHour(),m_curTime.GetMinute());
strIsFileName=m_inNameDlg.m_strInputEdit+strInputAdded;
CFile f2;
f2.Open(getMyDir()+"backedCab.mqy",CFile::modeCreate|CFile::modeWrite);
CArchive ar2(&f2,CArchive::store);
ar2<
for(int j=0;j
ar2<
ar2<
ar2.Close();
f2.Close();
m_savedNum++;
DeleteFile(toShortPath(m_strWinDir)+"sysbckup
b000.cab");
CStdioFile f; //建立包含系统备份命令的批处理文件并执行.
f.Open(getMyDir()+"myTemp.bat", CFile::modeCreate|CFile::modeWrite,NULL);
CString m_strCommand=toShortPath(m_strWinDir)+"commandscanreg.exe/backupn";
f.WriteString(m_strCommand);
f.Close();
WinExec(getMyDir()+"myTemp.bat",SW_HIDE);
CString m_toRbName;
m_toRbName.Format("rb0%d.cab", m_savedNum); //格式化存储文件名.
CTime m_beginTime=CTime::GetCurrentTime();
CTimeSpan m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
int delayTime=150/sysInfo.wProcessorLevel; //根据计算机的速度算出大致的完成时间.
while(!CopyFile(m_strWinDir+"sysbckup
b000.cab",getMyDir()+m_toRbName,0))
{
MSG msg;
if(::PeekMessage(&msg,m_hWnd,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
if(!(m_progress.GetPos()>=100))
m_progress.SetPos(100*m_timeSpan.GetSeconds()/delayTime);
m_timeSpan=CTime::GetCurrentTime()-m_beginTime;
}
m_progress.SetPos(100);
AfxMessageBox("已经成功的备份了系统文件");
DeleteFile(getMyDir()+"myTemp.bat");
}
更多精彩
赞助商链接