windows mobile ,wince 系统,用代码启动cab文件安装

有时候需要用代码来启动安装cab,以下是代码。不能实现静默安装。

启动后会提示用户是否安装,需要用户点击是才行。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
 
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
public class BLLInstallCab
{
 
    #region Const
 
    private const int STILL_ACTIVE = 0x103;
 
    #endregion
 
    #region P/Invoke
 
    [DllImport("coredll.dll", EntryPoint = "CreateProcess", SetLastError = true)]
    private static extern bool CreateProcess(string pszImageName, string pszCmdLine, IntPtr psaProcess, IntPtr psaThread, int fInheritHandles, int fdwCreate, IntPtr pvEnvironment, IntPtr pszCurDir, IntPtr psiStartInfo, ProcessInfo pi);
 
    [DllImport("coredll.dll", SetLastError = true)]
    private static extern bool GetExitCodeProcess(int hProcess, ref int lpExitCode);
 
    #endregion
 
    public sealed class ProcessInfo
    {
        public IntPtr hProcess = IntPtr.Zero;
        public IntPtr hThread = IntPtr.Zero;
        public int dwProcessID = 0;
        public int dwThreadID = 0;
    }
 
    /// <summary>
    /// 安装指定目录下多Cab包
    /// </summary>
    /// <param name="SetupDir">Cab包目录路径</param>
    public void SetupFiles(string SetupDir)
    {
        if (System.IO.Directory.Exists(SetupDir) == true)
        {
            ProcessInfo pi = new ProcessInfo();
            DirectoryInfo DirInfo = new DirectoryInfo(SetupDir);
            FileInfo[] Files = DirInfo.GetFiles("*.cab");
            foreach (FileInfo file in Files)
            {
                bool rc = CreateProcess("windows\\wceload.exe", "\"" + file.FullName + "\" /nodelete",
                    IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pi);
 
                int lpExitCode = STILL_ACTIVE;
 
                int ErrorCode = 0;
 
                while ((rc == true) && (lpExitCode == STILL_ACTIVE))
                {
                    Application.DoEvents();
                    rc = GetExitCodeProcess(pi.hProcess.ToInt32(), ref lpExitCode);
                    if (rc == true)
                    {
                        if (lpExitCode == STILL_ACTIVE)
                            System.Threading.Thread.Sleep(1000);
                    }
                    else
                    {
                        ErrorCode = Marshal.GetLastWin32Error();
                    }
                }
            }
        }
    }
 
    /// <summary>
    /// 检查系统安装CF版本
    /// </summary>
    /// <param name="version">版本</param>
    /// <returns></returns>
    //private bool HaveNETCF2(char version)
    //{
    //    RegistryKey NETCFKey = null;
    //    try
    //    {
    //        bool Result = true;
    //        NETCFKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\.NETCompactFramework", false);
    //        if (NETCFKey == null)
    //            return Result;
    //        string[] valueNames = NETCFKey.GetValueNames();
    //        if (valueNames == null)
    //        {
    //            NETCFKey.Close();
    //            return Result;
    //        }
    //        for (int i = 0; i < valueNames.Length; i++)
    //        {
    //            //枚举注册表Software\\Microsoft\\.NETCompactFramework\CF版本值
    //            if ((valueNames[i] != null) && (valueNames[i].Length > 0) && (valueNames[i][0] == version))
    //            {
    //                Result = true;
    //                break;
    //            }
    //            else
    //            {
    //                Result = false;
    //            }
    //        }
    //        return Result;
    //    }
    //    catch
    //    {
    //        return false;
    //    }
    //    finally
    //    {
    //        if (NETCFKey != null)
    //            NETCFKey.Close();
    //    }
    //}
}

  

posted @   LoveCoder  阅读(668)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示