客户端.net版本由3.5升级到4.5,首先把.net4.5的离线安装包添加到资源,程序运行的时候,从资源中生成离线安装包,并通过传递参数的方式执行静默安装命令,具体代码如下:

  

 1         private static void InstallDotNet()
 2         {
 3             Version vneed = new Version("4.5");
 4             bool needsetupdotnet = false;
 5             RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
 6             if (componentsKey == null)
 7             {
 8                 needsetupdotnet = true;
 9             }
10             else
11             {
12 
13                 Version vcurrent = new Version(componentsKey.GetValue("Version").ToString());
14                 if (vcurrent < vneed)
15                 {
16                     needsetupdotnet = true;
17                 }
18             }
19             if (needsetupdotnet)//需要安装
20             {
21                 PropertyInfo dotnetpinfo = res.GetType().GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance).Where(t => t.Name == "NDP452_KB2901907_x86_x64_AllOS_ENU").FirstOrDefault();
22                 byte[] dotnetBytevalue = (byte[])dotnetpinfo.GetValue(res, null);
23                 var dotnetfilePath = destpath + Path.DirectorySeparatorChar + "NDP452_KB2901907_x86_x64_AllOS_ENU.exe";
24                 if (!Directory.Exists(Path.GetDirectoryName(dotnetfilePath)))
25                 {
26                     Directory.CreateDirectory(Path.GetDirectoryName(dotnetfilePath));
27                 }
28                 using (FileStream writer = new FileStream(dotnetfilePath, FileMode.OpenOrCreate))
29                 {
30                     writer.Write(dotnetBytevalue, 0, dotnetBytevalue.Length);
31                 }
32                 RunExe(dotnetfilePath, "", "/Q /NORESTART /lcid 1033");
33             }
34         }
35 
36         public static string RunExe(string filename, string strInput, string arguments)
37         {
38             Process p = new Process();
39             //设置要启动的应用程序
40             //p.StartInfo.FileName = "cmd";
41             p.StartInfo.FileName = filename;
42             //是否使用操作系统shell启动
43             p.StartInfo.UseShellExecute = false;
44             // 接受来自调用程序的输入信息
45             p.StartInfo.RedirectStandardInput = true;
46             //输出信息
47             p.StartInfo.RedirectStandardOutput = true;
48             //输出错误
49             p.StartInfo.RedirectStandardError = true;
50             //不显示程序窗口
51             p.StartInfo.CreateNoWindow = true;
52 
53             if (!string.IsNullOrEmpty(arguments))
54             {
55                 p.StartInfo.Arguments = arguments;
56             }
57             //启动程序
58             p.Start();
59 
60             //向窗口发送输入信息
61             if (!string.IsNullOrEmpty(strInput))
62             {
63                 p.StandardInput.WriteLine(strInput + "&exit");
64             }
65             p.StandardInput.AutoFlush = true;
66             //获取输出信息
67             string strOuput = p.StandardOutput.ReadToEnd();
68             //等待程序执行完退出进程
69             p.WaitForExit();
70             p.Close();
71             return strOuput;
72         }        

 

posted on 2018-07-13 14:53  二豆  阅读(888)  评论(0编辑  收藏  举报