Posted on 2008-12-16 17:56
風(fēng)神 閱讀(2548)
評(píng)論(2) 編輯 收藏 引用 所屬分類(lèi):
程序點(diǎn)滴
以前做了個(gè)自動(dòng)更新程序,后來(lái)把這個(gè)自動(dòng)更新程序更新了,但是當(dāng)時(shí)沒(méi)有設(shè)計(jì)讓自動(dòng)更新程序來(lái)更新自己,這次就把這個(gè)功能加到里面了。在添加這個(gè)功能的時(shí)候,在網(wǎng)上搜了一下,已經(jīng)有很多這方面的資料,我最后用了批處理來(lái)完成的。
設(shè)計(jì)思路:
1.自動(dòng)更新程序檢測(cè)到網(wǎng)上有新版本的自己時(shí),先從網(wǎng)上下載新版本程序到同一個(gè)目錄下,另起個(gè)名字保存。
2.在自動(dòng)更新程序退出時(shí),創(chuàng)建并運(yùn)行一個(gè)批處理文件,來(lái)完成以舊換新的功能。
下面是相關(guān)的實(shí)現(xiàn)部分
1 bool CAutoUpdateDlg::DeleteMyself(void)
2 {
3 //1.創(chuàng)建自己批處理文件
4 CString sbatName,sPath;
5 sPath=m_strAppPath+m_strAppName;
6 sbatName=m_strAppPath+"delete.bat";
7 ofstream outfile(sbatName.GetBuffer());
8 if(outfile)
9 {
10 outfile<<":try"<<endl; //定義標(biāo)記
11 outfile<<"choice /t 1 /d y >nul"<<endl; //暫停1秒
12 outfile<<"del \""+sPath+"\""<<endl; //刪除原程序文件
13 outfile<<"if exist \""+sPath+"\""+" goto :try"<<endl; //如果刪除失敗,運(yùn)行到標(biāo)記try處,循環(huán)以上步驟
14 outfile<<"rename "+m_strAppBakName+" "+m_strAppName<<endl; //重命名新文件為程序文件
15 outfile<<"del \""+sbatName+"\""; //刪除批處理文件
16 }
17 outfile.close();
18
19 //2.創(chuàng)建運(yùn)行批處理的進(jìn)程,它以空閑優(yōu)先級(jí)創(chuàng)建
20 STARTUPINFO si;
21 PROCESS_INFORMATION pi;
22 ZeroMemory( &si, sizeof(si) );
23 si.cb = sizeof(si);
24 si.dwFlags=STARTF_USESHOWWINDOW;
25 si.wShowWindow=SW_HIDE; //以隱藏狀態(tài)運(yùn)行
26 ZeroMemory( &pi, sizeof(pi) );
27 if( !CreateProcess( NULL, // No module name (use command line).
28 sbatName.GetBuffer(), // Command line.
29 NULL, // Process handle not inheritable.
30 NULL, // Thread handle not inheritable.
31 FALSE, // Set handle inheritance to FALSE.
32 IDLE_PRIORITY_CLASS, // IDLE flags.
33 NULL, // Use parent's environment block.
34 NULL, // Use parent's starting directory.
35 &si, // Pointer to STARTUPINFO structure.
36 &pi ) // Pointer to PROCESS_INFORMATION structure.
37 )
38 {
39 CloseHandle(pi.hThread);
40 CloseHandle(pi.hProcess);
41 return false;
42 }
43 return true;
44 }
程序經(jīng)過(guò)我的一番測(cè)試,暫時(shí)沒(méi)有出現(xiàn)不良反應(yīng)。希望對(duì)有這方面需求的朋友能有所借鑒,程序?qū)懙谋容^簡(jiǎn)單,如果有什么改進(jìn)的地方或是有更好的辦法,希望能及時(shí)的告知,謝謝。