Web 安装包实现IE的自动安装
1.创建自定义安装程序集SetupLibrary
2. 代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using Microsoft.Win32;
namespace SetupLibrary
{
[RunInstaller(true)]
public partial class SetupClass : Installer
{
public SetupClass()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
InstallIE8();
}
private static void InstallIE8()
{
try
{
if (OldVersionIEThan8())
{
var rarPro = new Process();
rarPro.StartInfo.FileName = "C:/Inetpub/wwwroot/AFE/IE8-WindowsXP-x86-CHS.exe";
rarPro.StartInfo.UseShellExecute = false;
rarPro.StartInfo.CreateNoWindow = false;
rarPro.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
rarPro.Start(); //开始
rarPro.WaitForExit(); //等待退出
rarPro.Dispose();
}
}catch(Exception e)
{
ShowMessage("安装IE8失败\n" + e.Message.ToString());
}
}
//这里的IIS安装没有用到
private static void InstallIIs()
{
RegistryKey key;
try
{
var installPath = "IIS/IIS6.0";
if (IsXPOS())
installPath = "IIS/IIS5.1";
ShowMessage(installPath);
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Setup", true);
if (key == null)
{
ShowMessage("Registry.LocalMachine.OpenSubKey(@\"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\", true)");
return;
}
//Convert.ToString(key.GetValue("SourcePath"));
//Convert.ToString(key.GetValue("ServicePackSourcePath"));
key.SetValue("ServicePackSourcePath", installPath);
key.SetValue("SourcePath", installPath);
var rarPro = new Process();
rarPro.StartInfo.FileName = "Sysocmgr.exe";
rarPro.StartInfo.Arguments = string.Format("/i:sysoc.inf /u:\"{0}\"", "iisTxt");
rarPro.StartInfo.UseShellExecute = false;
rarPro.StartInfo.CreateNoWindow = false;
rarPro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
rarPro.Start();//开始
rarPro.WaitForExit();//等待退出
rarPro.Dispose();
return;
}
catch (Exception err)
{
ShowMessage(err.Message);
}
finally
{
//key.SetValue("ServicePackSourcePath", servicePackSourcePath);
//key.SetValue("SourcePath", sourcePath);
}
return;
}
public override void Uninstall(IDictionary savedState)
{
}
private static bool IsXPOS()
{
return (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major == 5
&& Environment.OSVersion.Version.Minor ==1);
}
private static bool OldVersionIEThan8()
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer", true);
string version = "Version Vector";
if (IsXPOS())
{
if (key != null) version = Convert.ToString(key.GetValue("Version"));
}
return (version.Split('.')[0] != "8");
}
private static void ShowMessage(string txt)
{
var msgBox = new Message();
msgBox.SetMessageText(txt);
msgBox.ShowDialog();
}
}
}
3.在安装项目中配置自定义操作