static private (Version, string) GetVersionAndChecksum(WebClientEx client, bool useGitHub) { SystemManager.CheckServerCertificate(useGitHub ? Globals.CheckUpdateGitHubURL : Globals.CheckUpdateURL, useGitHub, true); List <string> lines; try { string path = useGitHub ? Globals.CheckUpdateGitHubURL : Globals.CheckUpdateURL; lines = client.DownloadString(path).SplitNoEmptyLines(useGitHub).Take(2).ToList(); } catch (Exception ex) { throw new WebException(SysTranslations.CheckUpdateReadError.GetLang(ex.Message)); } LoadingForm.Instance.DoProgress(); var list = new NullSafeOfStringDictionary <string>(); foreach (string line in lines) { var parts = line.Split(':'); if (parts.Length != 2) { continue; } list.Add(parts[0].Trim(), parts[1].Trim()); } string fileVersion = list["Version"]; string fileChecksum = list["Checksum"]; if (fileVersion.IsNullOrEmpty() || fileChecksum.IsNullOrEmpty()) { throw new WebException(SysTranslations.CheckUpdateFileError.GetLang(lines.AsMultiLine())); } var partsVersion = fileVersion.Split('.'); Version version; try { version = partsVersion.Length switch { 2 => new Version(Convert.ToInt32(partsVersion[0]), Convert.ToInt32(partsVersion[1])), 3 => new Version(Convert.ToInt32(partsVersion[0]), Convert.ToInt32(partsVersion[1]), Convert.ToInt32(partsVersion[2])), 4 => new Version(Convert.ToInt32(partsVersion[0]), Convert.ToInt32(partsVersion[1]), Convert.ToInt32(partsVersion[2]), Convert.ToInt32(partsVersion[3])), _ => throw new ArgumentException(SysTranslations.CheckUpdateFileError.GetLang(lines.AsMultiLine())), }; } catch (Exception ex) { throw new WebException(SysTranslations.CheckUpdateFileError.GetLang(lines.AsMultiLine()) + Globals.NL2 + ex.Message); } LoadingForm.Instance.DoProgress(); return(version, fileChecksum); }
/// <summary> /// Creates the filters for dialog boxes. /// </summary> /// <typeparam name="T"></typeparam> static public string CreateFilters <T>(this NullSafeOfStringDictionary <T> values) where T : struct, Enum { string str = SysTranslations.FileExtensionFilter.GetLang(); var list = values.Select(v => $"{string.Format(str, v.Key)}|*{v.Value}"); return(string.Join("|", list)); }
/// <summary> /// Sets the targets that are not supported. /// </summary> static public NullSafeOfStringDictionary <T> SetUnsupported <T>(this NullSafeOfStringDictionary <T> values, params T[] list) where T : struct, Enum { foreach (var pair in values.Where(p => list.Contains(p.Key)).ToList()) { values.Remove(pair.Key); } return(values); }
static public bool LoadKeyValuePairs( this NullSafeOfStringDictionary <string> list, string filePath, string separator, bool showError = true) { try { list.Clear(); foreach (string line in File.ReadAllLines(filePath)) { if (!line.IsCommentedText()) { var parts = line.SplitNoEmptyLines(separator); if (parts.Length == 1) { list.Add(parts[0].Trim(), string.Empty); } else if (parts.Length == 2) { list.Add(parts[0].Trim(), parts[1].Trim()); } else if (parts.Length > 2) { list.Add(parts[0].Trim(), parts.Skip(1).Join(separator)); } } } return(true); } catch (FileNotFoundException) { if (showError) { MessageBox.Show(SysTranslations.FileNotFound.GetLang(filePath), Globals.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(false); } catch (Exception ex) { if (showError) { MessageBox.Show(SysTranslations.LoadFileError.GetLang(filePath, ex.Message), Globals.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(false); } }
/// <summary> /// Fills a combo box with the specified list of targets. /// </summary> /// <param name="combobox">The combobox.</param> /// <param name="list">The list.</param> /// <param name="valueDefault">The default value.</param> static public void Fill <T>(this ComboBox combobox, NullSafeOfStringDictionary <T> list, T valueDefault) where T : struct, Enum { foreach (KeyValuePair <T, string> item in list) { int index = combobox.Items.Add(item); if (item.Key.Equals(valueDefault)) { combobox.SelectedIndex = index; } } }
/// <summary> /// Creates the export targets. /// </summary> /// <typeparam name="T"></typeparam> static public NullSafeOfStringDictionary <T> CreateExportTargets <T>(params T[] list) where T : struct, Enum { var result = new NullSafeOfStringDictionary <T>(); foreach (var value in Enums.GetValues <T>()) { if (list.Length == 0 || list.Contains(value)) { result.Add(value, "." + value.ToString().ToLower()); } } return(result); }
static public bool SaveKeyValuePairs(this NullSafeOfStringDictionary <string> list, string filePath, string separator, bool showError = true) { using var stream = File.CreateText(filePath); try { foreach (var item in list) { if (item.Key.IsCommentedText()) { stream.WriteLine(item.Key); } else { stream.WriteLine(item.Key + separator + item.Value); } } return(true); } catch (FileNotFoundException) { if (showError) { MessageBox.Show(SysTranslations.FileNotFound.GetLang(filePath), Globals.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(false); } catch (Exception ex) { if (showError) { MessageBox.Show(SysTranslations.LoadFileError.GetLang(filePath, ex.Message), Globals.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(false); } finally { stream.Close(); } }
/// <summary> /// Static constructor. /// </summary> static Languages() { try { Managed = Enums.GetValues <Language>().Skip(1).ToArray(); Codes = new NullSafeOfStringDictionary <Language>(Managed.ToDictionary(v => v, v => v.ToString().ToLower())); Values = new NullSafeOfEnumDictionary <string, Language>(Codes.ToDictionary(v => v.Value, v => v.Key)); } catch (Exception ex) { string str = "Exception in Language static class constructor." + Globals.NL2 + "Please contact support."; var einfo = new ExceptionInfo(null, ex); if (!einfo.ReadableText.IsNullOrEmpty()) { str += Globals.NL2 + einfo.ReadableText; } DisplayManager.ShowAndTerminate(str); } }
static public void Export(this DataTable table, string filePath, NullSafeOfStringDictionary <DataExportTarget> targets) { string extension = Path.GetExtension(filePath); var selected = targets.First(p => p.Value == extension).Key; switch (selected) { case DataExportTarget.TXT: using (var stream = File.CreateText(filePath)) foreach (DataRow row in table.Rows) { foreach (DataColumn column in table.Columns) { stream.WriteLine($"{column.ColumnName} = {row[column]}"); } stream.WriteLine(); } break; case DataExportTarget.CSV: var options = new CsvOptions("String[,]", Globals.CSVSeparator, table.Rows.Count) { IncludeHeaderNames = true, DateFormat = "yyyy-MM-dd HH:mm", Encoding = Encoding.UTF8 }; CsvEngine.DataTableToCsv(table, filePath, options); break; case DataExportTarget.JSON: var dataset = new DataSet(Globals.AssemblyTitle); dataset.Tables.Add(table); string lines = JsonConvert.SerializeObject(dataset, Formatting.Indented); File.WriteAllText(filePath, lines, Encoding.UTF8); dataset.Tables.Clear(); dataset.Dispose(); break; default: throw new AdvNotImplementedException(selected); } }
/// <summary> /// Gets the image format from a string extension. /// </summary> /// <param name="list">The list.</param> /// <param name="extension">The extension.</param> static public ImageFormat GetFormat(this NullSafeOfStringDictionary <ImageExportTarget> list, string extension) { return(list.FirstOrDefault(v => v.Value == extension).Key.GetFormat()); }