public async Task LoadPresetsForPlugin(Plugin plugin) { var presetsStorageFile = GetPresetsStorageFile(plugin); if (!File.Exists(presetsStorageFile)) { presetsStorageFile = FindPresetStorageFileById(plugin.PluginId); if (presetsStorageFile == null) { return; } } if (File.Exists(presetsStorageFile)) { var presets = GetLoadSerializer() .Deserialize <EditableCollection <Preset> >(await AsyncFile.ReadAllBytesAsync(presetsStorageFile)); foreach (var preset in presets) { preset.Plugin = plugin; } plugin.Presets = presets; plugin.OnAfterCerasDeserialize(); } }
public async Task Default_BytesRead() { var bytes = new byte[10000]; var random = new Random(); random.NextBytes(bytes); var path = Path.Combine(readAllBytesTestFolder, nameof(Default_BytesRead)); Directory.CreateDirectory(readAllBytesTestFolder); File.WriteAllBytes(path, bytes); var result = await AsyncFile.ReadAllBytesAsync(path).ConfigureAwait(false); CollectionAssert.AreEqual(bytes, result); }
/// <summary> /// Read file into byte array. /// </summary> /// <param name="path">Absolute file path.</param> /// <param name="throwIfNotExists">Flag indicates to throw exception if file not exists.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns></returns> public static async Task <byte[]> ReadFileAsync(string path, bool throwIfNotExists = true, CancellationToken cancellationToken = default(CancellationToken)) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException(nameof(path)); } if (!File.Exists(path)) { if (throwIfNotExists) { throw new InvalidOperationException($"File {path} not found"); } else { return(null); } } return(await AsyncFile.ReadAllBytesAsync(path, cancellationToken)); }
public void CancellationToken_BytesRead() { var bytes = new byte[100000]; var random = new Random(); random.NextBytes(bytes); var path = Path.Combine(readAllBytesTestFolder, nameof(CancellationToken_BytesRead)); Directory.CreateDirectory(readAllBytesTestFolder); File.WriteAllBytes(path, bytes); var tokenSource = new CancellationTokenSource(); Assert.ThrowsAsync <TaskCanceledException>(async() => { tokenSource.Cancel(); var task = AsyncFile.ReadAllBytesAsync(path, tokenSource.Token); await task; }); }