private void install_Click(object sender, EventArgs e) { if (pathValue.Text.Equals("")) { MessageBox.Show("安装目录不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!Directory.Exists(pathValue.Text.Substring(0, 3))) { MessageBox.Show("指定的安装目录所在驱动器不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (Directory.Exists(pathValue.Text) && (Directory.GetFiles(pathValue.Text).Length != 0 || Directory.GetDirectories(pathValue.Text).Length != 0)) { MessageBox.Show("指定的安装目录必须为空目录!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //开始执行安装 mainTabPanel.SelectedIndex = 3; if (!Directory.Exists(pathValue.Text)) { Directory.CreateDirectory(pathValue.Text); } long totalSize = IOUtils.Get7zOriginSize(ConfigUtils.WORK_PLACE + "\\data.7z"); TerminalResult result = new TerminalResult(); TerminalUtils.RunCommandAsynchronously("7z", "x " + TextUtils.SurroundByDoubleQuotes(ConfigUtils.WORK_PLACE + "\\data.7z") + " -o" + TextUtils.SurroundByDoubleQuotes(pathValue.Text), result); int ratio = 0; string curFile; while (!result.Finished) { try { DirInfo info = new DirInfo(); BinaryUtils.GetDirectoryInfo(pathValue.Text, info); ratio = (int)((float)info.Size / totalSize * 100); if (ratio > 100) { ratio = 100; } List <string> fileList = info.FileList; if (fileList.Count > 0) { curFile = fileList[fileList.Count - 1]; currentFile.Text = "正在释放:" + curFile.Substring(curFile.LastIndexOf("\\") + 1); } } catch { //none } finally { processValue.Text = ratio + "%"; progressBar.Value = ratio; Application.DoEvents(); } } //安装完成,写入相应注册表和创建快捷方式 if (ConfigUtils.GlobalConfigure.GenerateShortcut && addDesktopIcon.Checked) { foreach (string sitem in ConfigUtils.GlobalConfigure.ShortcutList) { string[] shortInfo = sitem.Split('|'); //分割后得到分别是原文件名、快捷方式名和运行参数(可能没有参数) string originFilePath = pathValue.Text + "\\" + shortInfo[0]; string destDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + shortInfo[1]; if (shortInfo.Length == 2) { IOUtils.CreateShortcut(originFilePath, destDesktopPath); } else { IOUtils.CreateShortcut(originFilePath, destDesktopPath, shortInfo[2]); } if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title)) { Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title); } string destMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title + "\\" + shortInfo[1]; if (shortInfo.Length == 2) { IOUtils.CreateShortcut(originFilePath, destMenuPath); } else { IOUtils.CreateShortcut(originFilePath, destMenuPath, shortInfo[2]); } } } if (ConfigUtils.GlobalConfigure.AddBootOption && addBootOption.Checked) { RegUtils.OperateBootOption(ConfigUtils.GlobalConfigure.Title, pathValue.Text + "\\" + ConfigUtils.GlobalConfigure.MainEXE, true); } //如果生成卸载程序,则加入注册表程序信息 if (ConfigUtils.GlobalConfigure.GenerateUninstall) { AppUninstallInfo info = new AppUninstallInfo(); info.DisplayName = ConfigUtils.GlobalConfigure.Title; info.InstallPath = pathValue.Text; info.UninstallString = pathValue.Text + "\\uninstall.exe"; string iconPath = ConfigUtils.GlobalConfigure.MainEXE; if (iconPath.Equals("")) { iconPath = "uninstall.exe"; } info.DisplayIcon = pathValue.Text + "\\" + iconPath; info.EstimatedSize = totalSize / 1024; info.DisplayVersion = ConfigUtils.GlobalConfigure.Version; info.Publisher = ConfigUtils.GlobalConfigure.Publisher; RegUtils.OperateAppUninstallItem(info, true); if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title)) { Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title); } IOUtils.CreateShortcut(pathValue.Text + "\\uninstall.exe", Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title + "\\卸载程序"); } mainTabPanel.SelectedIndex = 4; }
private void ok_Click(object sender, EventArgs e) { uninstalling.Visible = true; ok.Enabled = false; cancel.Enabled = false; Configure cfg = JsonConvert.DeserializeObject <Configure>(File.ReadAllText(WORK_PLACE + "\\cfg.ezcfg")); if (!cfg.RunBeforeUnSetup.Equals("")) { state.Text = "执行卸载前命令..."; Application.DoEvents(); Environment.CurrentDirectory = appPath; string totalCommand = cfg.RunBeforeUnSetup; if (totalCommand.Contains(" ")) { string command = totalCommand.Substring(0, totalCommand.IndexOf(" ")); string args = totalCommand.Substring(totalCommand.IndexOf(" ") + 1); TerminalUtils.RunCommand(command, args); } else { Process.Start(totalCommand); } } if (cfg.GenerateShortcut) { state.Text = "移除快捷方式..."; Application.DoEvents(); try { if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + cfg.Title)) { Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + cfg.Title, true); } if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + cfg.Title + ".lnk")) { File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + cfg.Title + ".lnk"); } } catch { //none } } if (cfg.AddBootOption) { state.Text = "移除开机启动项..."; Application.DoEvents(); RegUtils.OperateBootOption(cfg.Title, "", false); } state.Text = "移除程序信息注册表..."; Application.DoEvents(); RegUtils.OperateAppUninstallItem(new AppUninstallInfo(cfg.Title, ""), false); state.Text = "移除文件..."; Application.DoEvents(); string[] dirList = Directory.GetDirectories(appPath); string[] fileList = Directory.GetFiles(appPath); foreach (string dir in dirList) { try { Directory.Delete(dir, true); } catch { //none } } foreach (string file in fileList) { if (!file.Equals(selfPath)) { try { File.Delete(file); } catch { //none } } } File.WriteAllText(WORK_PLACE + "\\delete.vbs", "wscript.sleep 1000*1\r\nset f=createobject(\"Scripting.FileSystemObject\")\r\nf.DeleteFolder(\"" + appPath + "\"),true\r\nf.DeleteFolder(\"" + WORK_PLACE + "\"),true\r\ncall MsgBox(\"卸载成功!\", 64, \"完成\")", Encoding.GetEncoding("gbk")); Process.Start(WORK_PLACE + "\\delete.vbs"); Application.Exit(); }
/// <summary> /// 安装时执行 /// </summary> public static void DoInstall() { //开始执行安装 if (!Directory.Exists(ConfigUtils.GlobalConfigure.InstallPath)) { Directory.CreateDirectory(ConfigUtils.GlobalConfigure.InstallPath); } TerminalResult result = new TerminalResult(); TerminalUtils.RunCommandAsynchronously("7z", "x " + TextUtils.SurroundByDoubleQuotes(ConfigUtils.WORK_PLACE + "\\data.7z") + " -o" + TextUtils.SurroundByDoubleQuotes(ConfigUtils.GlobalConfigure.InstallPath), result); //安装完成,写入相应注册表和创建快捷方式 if (ConfigUtils.GlobalConfigure.GenerateShortcut) { foreach (string sitem in ConfigUtils.GlobalConfigure.ShortcutList) { string[] shortInfo = sitem.Split('|'); //分割后得到分别是原文件名、快捷方式名和运行参数(可能没有参数) string originFilePath = ConfigUtils.GlobalConfigure.InstallPath + "\\" + shortInfo[0]; string destDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + shortInfo[1]; if (shortInfo.Length == 2) { IOUtils.CreateShortcut(originFilePath, destDesktopPath); } else { IOUtils.CreateShortcut(originFilePath, destDesktopPath, shortInfo[2]); } if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title)) { Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title); } string destMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title + "\\" + shortInfo[1]; if (shortInfo.Length == 2) { IOUtils.CreateShortcut(originFilePath, destMenuPath); } else { IOUtils.CreateShortcut(originFilePath, destMenuPath, shortInfo[2]); } } } if (ConfigUtils.GlobalConfigure.AddBootOption) { RegUtils.OperateBootOption(ConfigUtils.GlobalConfigure.Title, ConfigUtils.GlobalConfigure.InstallPath + "\\" + ConfigUtils.GlobalConfigure.MainEXE, true); } //如果生成卸载程序,则加入注册表程序信息 if (ConfigUtils.GlobalConfigure.GenerateUninstall) { AppUninstallInfo info = new AppUninstallInfo(); info.DisplayName = ConfigUtils.GlobalConfigure.Title; info.InstallPath = ConfigUtils.GlobalConfigure.InstallPath; info.UninstallString = ConfigUtils.GlobalConfigure.InstallPath + "\\uninstall.exe"; string iconPath = ConfigUtils.GlobalConfigure.MainEXE; if (iconPath.Equals("")) { iconPath = "uninstall.exe"; } info.DisplayIcon = ConfigUtils.GlobalConfigure.InstallPath + "\\" + iconPath; info.EstimatedSize = IOUtils.Get7zOriginSize(ConfigUtils.WORK_PLACE + "\\data.7z") / 1024; info.DisplayVersion = ConfigUtils.GlobalConfigure.Version; info.Publisher = ConfigUtils.GlobalConfigure.Publisher; RegUtils.OperateAppUninstallItem(info, true); if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title)) { Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title); } IOUtils.CreateShortcut(ConfigUtils.GlobalConfigure.InstallPath + "\\uninstall.exe", Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms) + "\\" + ConfigUtils.GlobalConfigure.Title + "\\卸载程序"); } }