From 6dfc7bc4d46140b3596b900b581f2c4676bb032e Mon Sep 17 00:00:00 2001 From: Erik Ramfelt Date: Mon, 13 Apr 2015 17:15:40 +0200 Subject: [PATCH 1/2] Not relying on exception for program flow If I run our application with the library and would like to catch all exceptions not only those unhandled; then the Restore method will always throw an exception (if there id no file). With this change it will check if it exists and then return the default if it didnt exist. There is no need to try to read the stream from the file because it will not be there. --- .../CSharpAnalytics/Serializers/AppDataContractSerializer.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs b/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs index 1ce8243..5fa363c 100644 --- a/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs +++ b/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs @@ -52,6 +52,10 @@ public static async Task Restore(string filename = null, bool deleteBadDat try { var file = GetFilePath(filename); + if (!File.Exists(file)) + { + return default(T); + } try { From 3b69e99d2da17fca23756f306fb78ac0670ef841 Mon Sep 17 00:00:00 2001 From: Erik Ramfelt Date: Mon, 13 Apr 2015 17:25:45 +0200 Subject: [PATCH 2/2] Make it possible to change Folder path I would like to be able to set the base folder path to our application's local app data folder. Otherwise the files will not be removed when our application is removed. Since the method is public but the class is only internal, I made the class public. I know that we discussed that the default implementation could use the product name, etc. But this doesnt help because my code is loaded by a third party application, so that means that the files will be named like the application and not base on our codebase. --- Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs b/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs index 5fa363c..1e1ee61 100644 --- a/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs +++ b/Source/CSharpAnalytics/Serializers/AppDataContractSerializer.cs @@ -10,7 +10,7 @@ namespace CSharpAnalytics.Serializers /// Provides an easy way to serialize and deserialize simple classes to a user AppData folder in /// Windows Forms applications. /// - internal static class AppDataContractSerializer + public static class AppDataContractSerializer { private static string folderPath;