private bool processShortcut(ConfigRootObject config) { if (config.shortcut != null) { string iconFilePath = null; if (config.shortcut.iconFileNumber != null && config.files != null && config.shortcut.iconFileNumber <= config.files.Length - 1) { ConfigFile configFile; configFile = config.files[config.shortcut.iconFileNumber]; iconFilePath = Path.Combine(configFile.destination, configFile.name); } // Create desktop shortcut if (config.shortcut.createOnDesktop != null && config.shortcut.createOnDesktop) { CardonerSistemas.FileSystem.ShortcutAddToDesktop(Application.ExecutablePath, Application.StartupPath, config.shortcut.displayName, iconFilePath); } // Create start menu shortcut if (config.shortcut.createOnStartMenu != null && config.shortcut.createOnStartMenu) { CardonerSistemas.FileSystem.ShortcutAddToStartMenu(Application.ExecutablePath, Application.StartupPath, config.shortcut.startMenuFolder, config.shortcut.displayName, iconFilePath); } } progressbarStatus.Value++; // This is a workaround for the progress bar to show when occurs in small time progressbarStatus.Value--; progressbarStatus.Value++; Application.DoEvents(); return(true); }
/// <summary> /// 读取 config.json /// </summary> private void ReadConfigJson() { Log.WriteLog(Log.LogLevel.信息, "开始读取 config.json"); if (File.Exists(@".\GiMiSiTemp\config.json")) { // 打开文件 FileStream fileStream = new FileStream(@".\GiMiSiTemp\config.json", FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); // 把 byte[] 转换成 Stream Stream stream = new MemoryStream(bytes); var configString = new StreamReader(stream, Encoding.UTF8).ReadToEnd(); var temp = JsonConvert.DeserializeObject <ConfigRootObject>(configString); if (temp != null) { configRootObject = temp; } stream.Close(); } Log.WriteLog(Log.LogLevel.信息, "读取 config.json 完成"); var gms2RuntimeRss = configRootObject.GMS2RuntimeRss; switch (gms2RuntimeRss) { case "0": gms2RuntimeRssYoYoRadioButton.IsChecked = true; break; case "1": gms2RuntimeRssLiarOnceRadioButton.IsChecked = true; break; case "2": gms2BetaRuntimeRssYoYoRadioButton.IsChecked = true; break; case "3": gms2BetaRuntimeRssLiarOnceRadioButton.IsChecked = true; break; default: CustomURLTextBox.IsEnabled = true; CustomURLButton.IsEnabled = true; CustomURLTextBox.Text = gms2RuntimeRss; Global.GMS2RuntimeRss = new Uri(gms2RuntimeRss); gms2RuntimeRssCustomRadioButton.IsChecked = true; break; } }
private bool readConfigFile(ref ConfigRootObject config) { string[] commandLineArguments; string configFilename; string jsonConfigFileString; commandLineArguments = Environment.GetCommandLineArgs(); if (commandLineArguments.Length == 1) { configFilename = defaultConfigFilename; } else { configFilename = commandLineArguments[1]; } try { showStatusText("Leyendo archivo de configuración...", "", false); jsonConfigFileString = File.ReadAllText(configFilename); showStatusText("", "OK", false); } catch (Exception ex) { showStatusText("", "ERROR", false, true); MessageBox.Show($"Ha ocurrido un error al leer el archivo de configuración ({configFilename})\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } try { showStatusText("Interpretando archivo de configuración...", ""); config = JsonSerializer.Deserialize <ConfigRootObject>(jsonConfigFileString); showStatusText("", "OK", false); } catch (Exception ex) { showStatusText("", "ERROR", false, true); MessageBox.Show($"Ha ocurrido un error al interpretar el archivo de configuración ({configFilename})\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
private bool processFiles(ConfigRootObject config) { if (config.files != null) { foreach (ConfigFile configFile in config.files) { if (!processFile(configFile, config.commonSource, config.commonDestination)) { return(false); } progressbarStatus.Value++; // This is a workaround for the progress bar to show when occurs in small time progressbarStatus.Value--; progressbarStatus.Value++; Application.DoEvents(); } } return(true); }
private bool executeFile(ConfigRootObject config) { if (config.executeFileNumber != null && config.files != null && config.executeFileNumber <= config.files.Length - 1) { ConfigFile configFile; string executeFilePath; configFile = config.files[config.executeFileNumber]; executeFilePath = Path.Combine(configFile.destination, configFile.name); try { System.Diagnostics.Process.Start(executeFilePath); } catch (System.Exception ex) { showStatusText("", "ERROR", false, true); MessageBox.Show($"Error al iniciar el archivo ({executeFilePath}).\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } return(true); }
private void startTheProcess() { ConfigRootObject config = null; if (readConfigFile(ref config)) { // Common source folder showStatusText("Verificando carpeta de origen...", ""); config.commonSource = CardonerSistemas.FileSystem.ProcessFolderName(config.commonSource, true); if (config.commonSource.Length == 0) { config.commonSource = Application.StartupPath; } else if (config.commonSource.StartsWith(".")) { config.commonSource = Path.Combine(Application.StartupPath, config.commonSource); } // Common destination folder showStatusText("Verificando carpeta de destino...", ""); config.commonDestination = CardonerSistemas.FileSystem.ProcessFolderName(config.commonDestination, true); if (config.commonDestination.Length == 0) { config.commonDestination = Application.StartupPath; } // Calculate progress bar values // Starts with 1 for reading config file progressbarStatus.Maximum = 1; // adds the numer of files to process if (config.files != null) { progressbarStatus.Maximum += config.files.Length; } // add 1 more if there is a shortcut if (config.shortcut != null) { progressbarStatus.Maximum++; } // Set the first value to progress bar because the first step (read config) is complete showStatusText("Procesando archivos...", ""); progressbarStatus.Value = 1; // This is a workaround for the progress bar to show when occurs in small time progressbarStatus.Value--; progressbarStatus.Value++; Application.DoEvents(); // Process all files if (!processFiles(config)) { return; } // Verify and create shortcuts if necessary processShortcut(config); // Finally, execute corresponding file if (executeFile(config)) { Application.Exit(); } } }