程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#應用程序運行時候檢測Framework安裝

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

編輯:C#入門知識

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

解決方案是:

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

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

 

[cpp]
// 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_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CStartUpDlg dialog 
 
CStartUpDlg::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 Win32 
    m_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_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CStartUpDlg message handlers 
 
BOOL CStartUpDlg::OnInitDialog() 

    CDialog::OnInitDialog(); 
 
    // Set the icon for this dialog.  The framework does this automatically 
    //  when the application's main window is not a dialog 
    SetIcon(m_hIcon, TRUE);         // Set big icon 
    SetIcon(m_hIcon, FALSE);        // Set small icon 
    OnbtnCheckFramework(); 
    // TODO: Add extra initialization here 
     
    return 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.0 
    if(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); 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved