private void ExportFileToXML(SerializableStorageInfo storageInfo, string destinationPath) { using (FileStream fileStream = new FileStream(destinationPath, FileMode.OpenOrCreate)) { XmlSerializer formatter = new XmlSerializer(storageInfo.GetType()); formatter.Serialize(fileStream, storageInfo); } }
public async Task <Unit> Handle(FileExportQuery request, CancellationToken cancellationToken) { var storageInfo = await _storageRepository.GetStorageInfo(); SerializableStorageInfo serializableStorageInfo = ConvertingHelper.GetSerializableStorageInfo(storageInfo); await _fileRepository.ExportFile(serializableStorageInfo, request.DestinationPath, request.Format); return(Unit.Value); }
public static SerializableStorageInfo GetSerializableStorageInfo(StorageInfo storageInfo) { SerializableStorageInfo serializableStorageInfo = new SerializableStorageInfo { CreationDate = storageInfo.CreationDate, UsedStorage = storageInfo.UsedStorage }; GetInnerFiles(serializableStorageInfo.Directories, storageInfo.Directories); return(serializableStorageInfo); }
public Task ExportFile(SerializableStorageInfo storageInfo, string destinationPath, string format) { string directoryPath = Path.GetDirectoryName(destinationPath); if (!Directory.Exists(directoryPath)) { throw new ArgumentException($"Directory '{directoryPath}' is not exists"); } if (string.IsNullOrWhiteSpace(format)) { format = FileFormats.Json; } switch (format) { case FileFormats.Json: { ExportFileToJSON(storageInfo, destinationPath); break; } case FileFormats.Xml: { ExportFileToXML(storageInfo, destinationPath); break; } default: { throw new ArgumentException($"Unknown format '{format}'"); } } return(Task.CompletedTask); }
private void ExportFileToJSON(SerializableStorageInfo storageInfo, string destinationPath) { string jsonString = JsonSerializer.Serialize(storageInfo); File.WriteAllText(destinationPath, jsonString); }