private async void SaveContent(Func <string> action) { var dialogController = await this.ShowProgressAsync("导出文本", "正在将本文内容导出..."); dialogController.SetIndeterminate(); using (var dialog = new CommonSaveFileDialog()) { dialog.DefaultExtension = ".yml"; dialog.Filters.Add(new CommonFileDialogFilter("Yaml 源文件", "*.yml")); if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { var file = new FileInfo(dialog.FileName); file.Check(); using (var fileStream = file.Open(FileMode.Truncate, FileAccess.Write)) using (var writer = new StreamWriter(fileStream, new UTF8Encoding(true))) { writer.WriteLine(await Task.Run(action)); } } } await dialogController.CloseAsync(); }
/// <summary> /// 将二进制内容以 <see cref="CompressionMode.Compress"/> 的方式写入到 <see cref="GZipStream"/> /// </summary> /// <param name="info">文件的存储路径信息</param> /// <param name="content">写入的内容</param> public static void GZipSave(FileInfo info, IEnumerable <byte> content) { info.Check(); using (var file = info.Open(FileMode.Truncate, FileAccess.Write)) { file.GZipSave(content); } }
/// <summary> /// 以 <see cref="CompressionMode.Decompress"/> 的方式从 <see cref="GZipStream"/> 中读取内容 /// </summary> /// <param name="info">文件的路径</param> /// <returns>文件内容</returns> public static string GZipRead(this FileInfo info) { info.Check(); using (var file = info.OpenRead()) using (var gzip = new GZipStream(file, CompressionMode.Decompress)) using (var reader = new StreamReader(gzip)) { return(reader.ReadToEnd()); } }
static public void Log(string path, string fileName, LogInfo <ExceptionInfo> logInfo) { var filePath = $"{path}/{fileName}"; var fileInfo = new FileInfo(filePath); fileInfo.Check(); using (var file = fileInfo.Open(FileMode.Append, FileAccess.Write)) { Log(file, logInfo); } }
private bool SaveProgress(EditProgress progress, string path) { var fileInfo = new FileInfo(path); fileInfo.Check(); try { var jsonString = JsonConvert.SerializeObject(progress); var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(jsonString)); GZipExtension.GZipSave(fileInfo, encoded); } catch (Exception) { return(false); } progress.Translations.Where(t => t.State != TranslationState.Changed).ForEach(t => t.State = TranslationState.Changed); return(true); }
public static void Check(string path) { var fileInfo = new FileInfo(path); fileInfo.Check(); }