C#應用程式運行時候檢測Framework安裝

來源:互聯網
上載者:User

 因為程式是放在Ukey(隨身碟)中運行,不是Setup打包程式,所以啟動時如果未安裝Framework不能直接運行.net的exe啟動程式,

解決方案是:

由C++寫的Startup.exe做啟動程式,同時檢測本機是否安裝Framework,如果沒有則有c++調用啟動安裝,安裝Framework結束後,啟動C#應用程式。

其中C++的檢測安裝啟動程式碼如下,VC++6.0實現,做了一個隱藏的form表單: 

 

// StartUpDlg.cpp : implementation file//#include "stdafx.h"#include "StartUp.h"#include "StartUpDlg.h"//#include "WinBase.h"//#include "shlobj.h"//#include "shellapi.h"#include "Tlhelp32.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CStartUpDlg dialogCStartUpDlg::CStartUpDlg(CWnd* pParent /*=NULL*/): CDialog(CStartUpDlg::IDD, pParent){//{{AFX_DATA_INIT(CStartUpDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CStartUpDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CStartUpDlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CStartUpDlg, CDialog)//{{AFX_MSG_MAP(CStartUpDlg)ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(btnCheckFramework, OnbtnCheckFramework)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CStartUpDlg message handlersBOOL CStartUpDlg::OnInitDialog(){CDialog::OnInitDialog();// Set the icon for this dialog.  The framework does this automatically//  when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small iconOnbtnCheckFramework();// TODO: Add extra initialization herereturn TRUE;  // return TRUE  unless you set the focus to a control}// If you add a minimize button to your dialog, you will need the code below//  to draw the icon.  For MFC applications using the document/view model,//  this is automatically done for you by the framework.void CStartUpDlg::OnPaint() {if (IsIconic()){//CPaintDC dc(this); // device context for painting//SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangle//int cxIcon = GetSystemMetrics(SM_CXICON);//int cyIcon = GetSystemMetrics(SM_CYICON);//CRect rect;//GetClientRect(&rect);//int x = (rect.Width() - cxIcon + 1) / 2;//int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icon//dc.DrawIcon(x, y, m_hIcon);}else{//CDialog::OnPaint();}static int i=2;if(i>0){i--;ShowWindow(SW_HIDE);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags//  the minimized window.HCURSOR CStartUpDlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}//檢測FrameWork版本LPSTR regeditVision[] ={//"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v2.0", //"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727", //"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v3.0", //"SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v3.5","SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4.0"};//檢查註冊表bool CheckFrameworkRegedit() {        bool result=TRUE;     //判斷註冊表是否存在     //for (int i=0;i<4;i++)     //{         HKEY ck;//註冊表的鍵  //檢查註冊表是否存在這個索引值        if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,regeditVision[0],0,KEY_ALL_ACCESS,&ck))          {                    RegCloseKey(ck);//關閉註冊表              result=TRUE;             //break;         }         else           {               result=FALSE;         }     //}     return result; }  //根據程式名稱檢測進程(工作管理員中的名稱),沒找到返回0,找到返回進程號DWORD GetProcessIdFromName(LPCTSTR name){PROCESSENTRY32 pe;DWORD id=0;HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);pe.dwSize=sizeof(PROCESSENTRY32);if(!Process32First(hSnapshot,&pe))return 0;while(1){pe.dwSize=sizeof(PROCESSENTRY32);if(Process32Next(hSnapshot,&pe)==FALSE)break;if(strcmp(pe.szExeFile,name)==0){id=pe.th32ProcessID;break;}}CloseHandle(hSnapshot);return id;}//檢測.Net Framework 4.0是否安裝,未安裝則進行安裝包調用void CStartUpDlg::OnbtnCheckFramework() {//檢查註冊表查看是否按照Framework4.0if(CheckFrameworkRegedit()){if(WinExec("xxx.exe", SW_SHOWNORMAL) == ERROR_FILE_NOT_FOUND)MessageBox("啟動xxx程式失敗!","提示",MB_OK);else{//進程掛起10s,監測xxx運行情況,如果已經關閉則結束本監測程式Sleep(10000);while(GetProcessIdFromName("xxx.exe")>0){Sleep(1000);}Sleep(2000);SendMessage(WM_CLOSE);}}else{if(MessageBox("應用程式需要.NET Framework 4.0安裝包支援,確定要安裝嗎?","提示",MB_OKCANCEL) == IDOK){WinExec("framework/dotNetFx40_Full_x86_x64.exe", SW_SHOWNORMAL);//進程掛起3s,開始監測Framework安裝情況,安裝結束後直接啟動xxx程式Sleep(3000);while(GetProcessIdFromName("dotNetFx40_Full_x86_x64.exe")>0){Sleep(1000);}//關閉Framework安裝結束if(CheckFrameworkRegedit()){if(WinExec("OrgCertificate.exe", SW_SHOWNORMAL) == ERROR_FILE_NOT_FOUND)MessageBox("啟動XXX程式失敗!","提示",MB_OK);else{//進程掛起10s,監測xxx運行情況,如果已經關閉則結束本監測程式Sleep(10000);while(GetProcessIdFromName("xxx.exe")>0){Sleep(1000);}Sleep(2000);SendMessage(WM_CLOSE);}}else{MessageBox("Framework 4.0安裝失敗,請重新安裝!","提示",MB_OK);}}SendMessage(WM_CLOSE);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.