From 1c65dd299d19bc1e0808c13a1c451656c62e2571 Mon Sep 17 00:00:00 2001 From: zum Date: Mon, 15 Apr 2024 13:51:34 +0400 Subject: [PATCH] lab6 Succes --- .../StorageCollection.cs | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/StorageCollection.cs b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/StorageCollection.cs index c17bf44..69f9051 100644 --- a/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectStormTrooper/ProjectStormTrooper/CollectionGenericObjects/StorageCollection.cs @@ -105,24 +105,25 @@ public class StorageCollection File.Delete(filename); } - StringBuilder sb = new(); - sb.Append(_collectionKey); + using FileStream fs = new(filename, FileMode.Create); + using StreamWriter streamWriter = new StreamWriter(fs); + streamWriter.Write(_collectionKey); foreach (KeyValuePair> value in _storages) { - sb.Append(Environment.NewLine); + streamWriter.Write(Environment.NewLine); // не сохраняем пустые коллекции if (value.Value.Count == 0) { continue; } - sb.Append(value.Key); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.GetCollectionType); - sb.Append(_separatorForKeyValue); - sb.Append(value.Value.MaxCount); - sb.Append(_separatorForKeyValue); + streamWriter.Write(value.Key); + streamWriter.Write(_separatorForKeyValue); + streamWriter.Write(value.Value.GetCollectionType); + streamWriter.Write(_separatorForKeyValue); + streamWriter.Write(value.Value.MaxCount); + streamWriter.Write(_separatorForKeyValue); foreach (T? item in value.Value.GetItems()) { @@ -132,15 +133,10 @@ public class StorageCollection continue; } - sb.Append(data); - sb.Append(_separatorItems); + streamWriter.Write(data); + streamWriter.Write(_separatorItems); } } - - using FileStream fs = new(filename, FileMode.Create); - using StreamWriter streamWriter = new StreamWriter(fs); - streamWriter.Write(sb.ToString()); - return true; }