图1
图1中IWshRuntimeLibray是在“引用”-->“添加引用”,选择“COM”标签页中,选择“Windows Script Host Object Model”
图2
图3
图4
图5
图6
图7
Install项目-->JLInstaller文件代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using Library.Application;
using System.Windows.Forms;
using System.IO;
namespace Install
{
[RunInstaller(true)]
public partial class JLInstaller : Installer
{
public JLInstaller()
{
InitializeComponent();
}
/// <summary>
/// 安装
/// </summary>
/// <param name="stateSaver"></param>
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
InstallApplication();
}
/// <summary>
/// 安装后
/// </summary>
/// <param name="savedState"></param>
protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
base.OnAfterInstall(savedState);
}
/// <summary>
/// 卸载
/// </summary>
/// <param name="savedState"></param>
public override void Uninstall(System.Collections.IDictionary savedState)
{
string _TargetDir = this.Context.Parameters["TargetDir"];
Logging.WriterInstallLog(_TargetDir, "开始执行卸载!");
try
{
//删除快捷
CreatingShortcuts.DeleteDesktopShortcuts();
CreatingShortcuts.DeleteProgramsShortcuts();
}
catch
{
Logging.WriterInstallLog(_TargetDir, "执行卸载失败!");
}
base.Uninstall(savedState);
}
/// <summary>
/// 卸载后
/// </summary>
/// <param name="savedState"></param>
protected override void OnAfterUninstall(System.Collections.IDictionary savedState)
{
string _TargetDir = this.Context.Parameters["TargetDir"];
try
{
//删除安装目录
CreatingShortcuts.DeleteStallerDir(_TargetDir);
}
catch
{
Logging.WriterInstallLog(_TargetDir, "执行卸载成功!");
}
base.OnAfterUninstall(savedState);
}
/// <summary>
/// 回滚
/// </summary>
/// <param name="savedState"></param>
public override void Rollback(IDictionary savedState)
{
//删除快捷
CreatingShortcuts.DeleteDesktopShortcuts();
CreatingShortcuts.DeleteProgramsShortcuts();
//string filePath = base.Context.Parameters["TargetDir"];
//Directory.Delete(filePath, true);
base.Rollback(savedState);
}
/// <summary>
/// 安装流程
/// </summary>
private void InstallApplication()
{
string filePath = base.Context.Parameters["TargetDir"];
Logging.WriterInstallLog(filePath, "--------------------------");
Logging.WriterInstallLog(filePath, "开始安装现金流通管理系统");
int num = 0;
try
{
//第三方控件注册
if (RegisterInstallInfo.RegisterAdd2Assembly(filePath))
{
Logging.WriterInstallLog(filePath, "注册成功!");
}
else
{
Logging.WriterInstallLog(filePath, "注册失败!");
num++;
}
//创建快捷方式
if (CreatingShortcuts.CreatingProgramMenuFolder(filePath) && CreatingShortcuts.CreatingShortcut(filePath))
{
Logging.WriterInstallLog(filePath, "成功创建快捷方式!");
}
else
{
Logging.WriterInstallLog(filePath, "创建快捷方式失败!");
num++;
}
if (num != 0)
{
throw new Exception("安装失败!");
}
else
{
Logging.WriterInstallLog(filePath, "安装成功!");
Logging.WriterInstallLog(filePath, "-----------------------");
}
}
catch (Exception exception)
{
Logging.WriterInstallLog(filePath, "安装失败!");
Logging.WriterInstallLog(filePath, "-----------------------");
Logging.WriterInstallLog(filePath, exception.ToString());
Logging.WriterInstallLog(filePath, "------------------------");
MessageBox.Show("安装失败,具体安装内容请查看安装日志InstallLogyyyymmdd.txt!");
throw new Exception("安装失败,安装程序已经停止!");
}
}
}
}
Library项目-->Application目录-->CreatingShortcuts文件代码:
using System;
using System.Collections.Generic;
using System.Text;
using IWshRuntimeLibrary;
namespace Library.Application
{
public class CreatingShortcuts
{
/// <summary>
/// 创建桌面快捷方式
/// </summary>
/// <param name="FilePath">程序安装路径</param>
public static bool CreatingShortcut(string FilePath)
{
bool Result = false;
try
{
WshShell shell = new WshShell();
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + "系统.lnk";
if (System.IO.File.Exists(FolderPath)) // 如果快捷方式已经存在,则删除
{
System.IO.File.Delete(FolderPath);
}
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath);
shortcut.TargetPath = FilePath + "WindowsFormsApplication2.exe";