当前位置:编程学习 > C#/ASP.NET >>

C#应用程序运行时候检测Framework安装

因为程序是放在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;  
        }  
 &

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,