static JSettings _Load(string file, Type type, bool useDefault) { JSettings R = null; if (AFile.ExistsAsAny(file)) { try { if (useDefault) { AFile.Delete(file); } else { var b = AFile.LoadBytes(file); var opt = new JsonSerializerOptions { IgnoreNullValues = true, AllowTrailingCommas = true }; R = JsonSerializer.Deserialize(b, type, opt) as JSettings; } } catch (Exception ex) { string es = ex.ToStringWithoutStack(); if (useDefault) { AOutput.Write($"Failed to delete settings file '{file}'. {es}"); } else { string backup = file + ".backup"; try { AFile.Move(file, backup, IfExists.Delete); } catch { backup = "failed"; } AOutput.Write( $@"Failed to load settings from {file}. Will use default settings. {es} Backup: {backup}" ); } } } R ??= Activator.CreateInstance(type) as JSettings; R._file = file; R._loaded = true; //autosave if (Interlocked.Exchange(ref s_loadedOnce, 1) == 0) { AThread.Start(() => { for (; ;) { Thread.Sleep(2000); _SaveAllIfNeed(); } }, sta: false); AProcess.Exit += (unu, sed) => _SaveAllIfNeed(); //info: Core does not call finalizers when process exits } lock (s_list) s_list.Add(R); return(R); }
private void _ButtonOK_Click(object sender, EventArgs e) { var ok = true; var path = textPath.Text; if (!APath.IsFullPath(path)) { ok = false; } else if (AFile.ExistsAsAny(path)) { ADialog.ShowError("Already exists", path, owner: this); ok = false; } this.DialogResult = ok ? DialogResult.OK : DialogResult.None; }
/// <summary> /// If <see cref="AVersion.Is32BitProcessAnd64BitOS"/> is true and path starts with <see cref="AFolders.System"/>, replaces that path part with <see cref="AFolders.SystemX64"/>. /// It disables redirection to <see cref="AFolders.SystemX86"/> for that path. /// </summary> /// <param name="path">Normalized path. This function does not normalize. Also it is unaware of <c>@"\\?\"</c>.</param> /// <param name="ifExistsOnlyThere">Don't replace path if the file or directory exists in the redirected folder or does not exist in the non-redirected folder.</param> public static string GetNonRedirectedSystemPath(string path, bool ifExistsOnlyThere = false) { int i = _IsSystem64PathIn32BitProcess(path); if (i == 0) { return(path); } if (ifExistsOnlyThere && AFile.ExistsAsAny(path)) { return(path); } var s = path.ReplaceAt(0, i, AFolders.SystemX64); if (ifExistsOnlyThere && !AFile.ExistsAsAny(s)) { return(path); } return(s); }