wpf制作自己的安装程序(可定制)
用wpf开发自己的安装程序,包括安装界面、卸载界面、创建快捷方式等
安装程序原理:
1、将编译好的文件打包成zip的压缩文件,
2、然后将zip以资源的方式内嵌到安装程序中
3、在安装的时候使用ICSharpCode.SharpZipLib.dll将zip文件解压到相应的目录中
4、建立相应的快捷方式,启动主程序程序
核心代码:
解压:
/// <summary> /// ZIP助手类 /// </summary> public static class ZIPHelper { public static Action<double, double, string> ActionProgress; /// <summary> /// 解压缩zip文件 /// </summary> /// <param name="zipFile">解压的zip文件流</param> /// <param name="extractPath">解压到的文件夹路径</param> /// <param name="bufferSize">读取文件的缓冲区大小</param> public static void Extract(byte[] zipFile, string extractPath, int bufferSize) { extractPath = extractPath.TrimEnd('/') + "//"; byte[] data = new byte[bufferSize]; int size;//缓冲区的大小(字节) double max = 0;//带待压文件的大小(字节) double osize = 0;//每次解压读取数据的大小(字节) using (ZipInputStream s = new ZipInputStream(new System.IO.MemoryStream(zipFile))) { ZipEntry entry; while ((entry = s.GetNextEntry()) != null) { max += entry.Size;//获得待解压文件的大小 } } using (ZipInputStream s = new ZipInputStream(new System.IO.MemoryStream(zipFile))) { ZipEntry entry; while ((entry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(entry.Name); string fileName = Path.GetFileName(entry.Name); //先创建目录 if (directoryName.Length > 0) { Directory.CreateDirectory(extractPath + directoryName); } if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(extractPath + entry.Name.Replace("/", "//"))) { while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { osize += size; System.Windows.Forms.Application.DoEvents(); streamWriter.Write(data, 0, size); string text = Math.Round((osize / max * 100), 0).ToString() + "%"; ActionProgress?.Invoke(max + 5, osize, text); System.Windows.Forms.Application.DoEvents(); } else { break; } } } } } } } }
创建快捷方式
/// <summary> /// 执行软件安装 /// </summary> private void Setup() { try { IsFinished = false; //获取用户选择路径中的最底层文件夹名称 string fileName = this.txtInstallationPath.Text.Split('\\')[this.txtInstallationPath.Text.Split('\\').Count() - 1]; //当用户选择的安装路径中最底层的文件夹名称不是“XthkDecryptionTool”时,自动在创建一个“XthkDecryptionTool”文件夹,防止在删除的时候误删别的文件 if (!fileName.Equals(InstallEntity.InstallFolderName)) { this.txtInstallationPath.Text = this.txtInstallationPath.Text + @"\" + InstallEntity.InstallFolderName; } //安装路径 InstallPath = this.txtInstallationPath.Text; //显示安装进度界面 //this.tcMain.SelectedIndex = 1; this.grid_one.Visibility = Visibility.Collapsed; this.grid_two.Visibility = Visibility.Visible; this.grid_three.Visibility = Visibility.Collapsed; //检测是否已经打开 Process[] procCoursewareDecryptionTool = Process.GetProcessesByName(InstallEntity.AppProcessName); if (procCoursewareDecryptionTool.Any()) { if (MessageBox.Show("“" + InstallEntity.DisplayName + "”正在运行中,是否强制覆盖程序?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) { Common.IsAppKill(InstallEntity.AppProcessName); } else { Application.Current.Shutdown(); } } //创建用户指定的安装目录文件夹 Directory.CreateDirectory(InstallPath); ZIPHelper.ActionProgress -= ActionProgressResult; ZIPHelper.ActionProgress += ActionProgressResult; this.pbSchedule.Value = 0; this.txtSchedule.Text = "0%"; //将软件解压到用户指定目录 ZIPHelper.Extract(Install.SetupFiles.Setup, InstallPath, 1024 * 1204); //将嵌入的资源释放到用户选择的安装目录下面(卸载程序) string uninstallPath = this.txtInstallationPath.Text + @"\" + InstallEntity.UninstallName; FileStream fsUninstall = System.IO.File.Open(uninstallPath, FileMode.Create); fsUninstall.Write(Install.SetupFiles.Uninstall, 0, Install.SetupFiles.Uninstall.Length); fsUninstall.Close(); //将嵌入的资源释放到用户选择的安装目录下面(快捷图标) string InstallIcoPath = this.txtInstallationPath.Text + InstallEntity.IconDirectoryPath; FileStream fsInstallIcoPath = System.IO.File.Open(InstallIcoPath, FileMode.Create); var InstallIco = Install.SetupFiles.IcoInstall; byte[] byInstall = Common.ImageToByteArray(InstallIco); fsInstallIcoPath.Write(byInstall, 0, byInstall.Length); fsInstallIcoPath.Close(); //将嵌入的资源释放到用户选择的安装目录下面(快捷卸载图标) string UninstallIcoPath = this.txtInstallationPath.Text + InstallEntity.UninstallIconDirectoryPath; FileStream fsUninStallIco = System.IO.File.Open(UninstallIcoPath, FileMode.Create); var UnInstallIco = Install.SetupFiles.IcoUninstall; byte[] byUnInstall = Common.ImageToByteArray(UnInstallIco); fsUninStallIco.Write(byUnInstall, 0, byUnInstall.Length); fsUninStallIco.Close(); //释放卸载程序完成,更新进度条 this.pbSchedule.Value = this.pbSchedule.Value + 1; this.txtSchedule.Text = Math.Round((this.pbSchedule.Value / this.pbSchedule.Maximum * 100), 0).ToString() + "%"; //添加开始菜单快捷方式 RegistryKey HKEY_CURRENT_USER = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"); string programsPath = HKEY_CURRENT_USER.GetValue("Programs").ToString();//获取开始菜单程序文件夹路径 Directory.CreateDirectory(programsPath + InstallEntity.MenuFolder);//在程序文件夹中创建快捷方式的文件夹 //更新进度条 this.pbSchedule.Value = this.pbSchedule.Value + 1; this.txtSchedule.Text = Math.Round((this.pbSchedule.Value / this.pbSchedule.Maximum * 100), 0).ToString() + "%"; //快捷方式名称"; string IconPath = InstallPath + InstallEntity.IconDirectoryPath; string UninstallIconPath = InstallPath + InstallEntity.UninstallIconDirectoryPath; string InstallExePath = InstallPath + @"\" + InstallEntity.AppExeName; string ExeUnInstallPath = InstallPath + @"\" + InstallEntity.UninstallName; //开始菜单打开快捷方式 shortName = programsPath + InstallEntity.MenuFolder + InstallEntity.ShortcutName; Common.CreateShortcut(shortName, InstallExePath, IconPath);//创建快捷方式 //更新进度条 this.pbSchedule.Value = this.pbSchedule.Value + 1; this.txtSchedule.Text = Math.Round((this.pbSchedule.Value / this.pbSchedule.Maximum * 100), 0).ToString() + "%"; //开始菜单卸载快捷方式 Common.CreateShortcut(programsPath + InstallEntity.MenuFolder + InstallEntity.UninstallShortcutName, ExeUnInstallPath, UninstallIconPath);//创建卸载快捷方式 //更新进度条 this.pbSchedule.Value = this.pbSchedule.Value + 1; this.txtSchedule.Text = Math.Round((this.pbSchedule.Value / this.pbSchedule.Maximum * 100), 0).ToString() + "%"; //添加桌面快捷方式 string desktopPath = HKEY_CURRENT_USER.GetValue("Desktop").ToString();//获取桌面文件夹路径 shortName = desktopPath + @"\" + InstallEntity.ShortcutName; Common.CreateShortcut(shortName, InstallExePath, IconPath);//创建快捷方式 //常见控制面板“程序与功能” //可以往root里面写,root需要管理员权限,如果使用了管理员权限,主程序也会以管理员打开,如需常规打开,需要在打开进程的时候做降权处理 RegistryKey CUKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32); var currentVersion = CUKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"); Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("DisplayIcon", InstallExePath);//显示的图标的exe dic.Add("DisplayName", InstallEntity.DisplayName);//名称 dic.Add("Publisher", InstallEntity.Publisher);//发布者 dic.Add("UninstallString", ExeUnInstallPath);//卸载的exe路径 dic.Add("DisplayVersion", InstallEntity.VersionNumber); RegistryKey CurrentKey = CUKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + InstallEntity.DisplayName, true); if (CurrentKey == null) { //说明这个路径不存在,需要创建 CUKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + InstallEntity.DisplayName); CurrentKey = CUKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\" + InstallEntity.DisplayName, true); } foreach (var item in dic) { CurrentKey.SetValue(item.Key, item.Value); } CurrentKey.Close(); //更新进度条 this.pbSchedule.Value = this.pbSchedule.Value + 1; this.txtSchedule.Text = Math.Round((this.pbSchedule.Value / this.pbSchedule.Maximum * 100), 0).ToString() + "%"; //安装完毕,显示结束界面 this.grid_one.Visibility = Visibility.Collapsed; this.grid_two.Visibility = Visibility.Collapsed; this.grid_three.Visibility = Visibility.Visible; IsFinished = true; } catch (Exception) { //安装完毕,显示结束界面 this.grid_one.Visibility = Visibility.Visible; this.grid_two.Visibility = Visibility.Collapsed; this.grid_three.Visibility = Visibility.Collapsed; throw; } }