private void Install() { if (!CheckBeforeInstall()) { return; } string package, extTarget, desktopPath, startupPath; // -- 1. 拷贝目录 -- package = UtilFile.Combine(ProjectConst.SourcePath, "package"); UtilFile.CopyDiretory(package, TargetPath); // -- 2. 创建快捷方式到桌面和启动组 -- extTarget = UtilFile.Combine(TargetPath, ProjectConst.APP_EXE); desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); CreateShortcut(TargetPath, extTarget, ProjectConst.APP_SHORTCUT, ProjectConst.APP_SHORTCUT_DESC, desktopPath); if (ProjectConst.FLAG_SHORTCUT_ON_STARTUP) { startupPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup); CreateShortcut(TargetPath, extTarget, ProjectConst.APP_SHORTCUT, ProjectConst.APP_SHORTCUT_DESC, startupPath); } // -- 4. 立即运行 -- if (UtilMessage.ShowConfirm(ProjectConst.APP_SHORTCUT_DESC + " 安装成功,是否立即运行?") == DialogResult.Yes) { Environment.CurrentDirectory = TargetPath; // -- 确保exeTarget运行时,当前目录是exeTarget所在目录,否则是SmartInstaller.exe所在目录 -- Process.Start(extTarget); } else { UtilMessage.ShowMessage(ProjectConst.APP_SHORTCUT_DESC + " 安装成功,已创建桌面快捷方式。"); } // -- 9. end -- this.Close(); }
private bool CheckBeforeInstall() { if (Directory.Exists(TargetPath)) { DirectoryInfo info = new DirectoryInfo(TargetPath); if (info.GetFiles().Length > 0) { if (UtilMessage.ShowConfirm("安装目标目录不为空,确定要覆盖安装吗?") != DialogResult.Yes) { return(false); } } } else { if (!UtilFile.CheckDirectory(TargetPath, true)) { UtilMessage.ShowWarning("创建安装目标目录失败,可能是当前用户权限不足,请检查。"); return(false); } } return(true); }