11using Newtonsoft . Json ;
2+ using System ;
23using System . Collections . Generic ;
34using System . IO ;
45using System . Windows . Forms ;
@@ -7,15 +8,17 @@ namespace PatternFileMover
78{
89 class NameAssociations
910 {
10- public static string _configPath = Application . StartupPath + Path . DirectorySeparatorChar + "nameAssociations.json" ;
11-
12- public static string configPath { get => _configPath ; }
11+ public static string _legacyConfigPath = Application . StartupPath + Path . DirectorySeparatorChar + "nameAssociations.json" ;
12+ public static string _configManifestPath = Application . StartupPath + Path . DirectorySeparatorChar + "manifest.json" ;
13+ public static string legacyConfigPath { get => _legacyConfigPath ; }
14+ public static string configManifestPath { get => _configManifestPath ; }
1315
1416 public static bool CreateEmptyConfigFile ( )
1517 {
1618 try
1719 {
18- File . WriteAllText ( configPath , JsonConvert . SerializeObject ( new List < NameAssociationsData > ( ) ) ) ;
20+ File . WriteAllText ( configManifestPath , "v2" ) ;
21+ File . WriteAllText ( GetConfigFilePath ( ) , JsonConvert . SerializeObject ( new List < NameAssociationsData_v2 > ( ) ) ) ;
1922 }
2023 catch ( System . ArgumentException )
2124 {
@@ -25,14 +28,81 @@ public static bool CreateEmptyConfigFile()
2528 return true ;
2629 }
2730
28- public static List < NameAssociationsData > LoadFromExistingConfigFile ( )
31+ public static string GetConfigFilePath ( )
32+ {
33+ return Application . StartupPath +
34+ Path . DirectorySeparatorChar +
35+ "nameAssociation_" +
36+ File . ReadAllText ( Path . GetFullPath ( configManifestPath ) ) +
37+ ".json" ;
38+ }
39+
40+ public static void checkAndUpgradeConfigurationFile ( ) {
41+ string manifestVersion = File . ReadAllText ( Path . GetFullPath ( configManifestPath ) ) ;
42+
43+ if ( ! File . Exists ( Path . GetFullPath ( GetConfigFilePath ( ) ) ) )
44+ {
45+ // we could not find a configuration file that matches the config manifest
46+ // lets check if we find an outdated file that should be updated
47+
48+ // version 1 (before implementing the file manifest)
49+ if ( File . Exists ( Path . GetFullPath ( legacyConfigPath ) ) )
50+ {
51+ List < NameAssociationsData_v1 > legacyValues = LoadFromExistingConfigFile_v1 ( legacyConfigPath ) ;
52+ List < NameAssociationsData_v2 > convertedValues = new List < NameAssociationsData_v2 > ( ) ;
53+
54+ foreach ( NameAssociationsData_v1 value in legacyValues ) {
55+ convertedValues . Add ( new NameAssociationsData_v2 ( )
56+ {
57+ Name = value . Name ,
58+ SearchPattern = value . SearchPattern ,
59+ TargetDirectory = value . TargetDirectory ,
60+ // version 1 only allows the processing of .pdf files
61+ FileExtension = ".pdf"
62+ } ) ;
63+ }
64+
65+ WriteByList ( convertedValues ) ;
66+ }
67+ }
68+ }
69+
70+ public static List < NameAssociationsData_v2 > LoadFromExistingConfigFile ( ) {
71+ return LoadFromExistingConfigFile_v2 ( ) ;
72+ }
73+
74+ public static List < NameAssociationsData_v1 > LoadFromExistingConfigFile_v1 ( string path = "" )
2975 {
30- return JsonConvert . DeserializeObject < List < NameAssociationsData > > ( File . ReadAllText ( Path . GetFullPath ( configPath ) ) ) ;
76+ if ( String . IsNullOrEmpty ( path ) )
77+ {
78+ path = GetConfigFilePath ( ) ;
79+ }
80+
81+ return JsonConvert . DeserializeObject < List < NameAssociationsData_v1 > > ( File . ReadAllText ( Path . GetFullPath ( path ) ) ) ;
82+ }
83+
84+ public static List < NameAssociationsData_v2 > LoadFromExistingConfigFile_v2 ( string path = "" )
85+ {
86+ if ( String . IsNullOrEmpty ( path ) )
87+ {
88+ path = GetConfigFilePath ( ) ;
89+ }
90+
91+ return JsonConvert . DeserializeObject < List < NameAssociationsData_v2 > > ( File . ReadAllText ( Path . GetFullPath ( path ) ) ) ;
3192 }
3293
33- public static void WriteByList ( List < NameAssociationsData > list )
94+ public static void WriteByList ( List < NameAssociationsData_v2 > list )
3495 {
35- File . WriteAllText ( configPath , JsonConvert . SerializeObject ( list ) ) ;
96+ File . WriteAllText (
97+ GetConfigFilePath ( ) ,
98+ JsonConvert . SerializeObject (
99+ list ,
100+ Formatting . None ,
101+ new JsonSerializerSettings {
102+ DefaultValueHandling = DefaultValueHandling . Include ,
103+ }
104+ )
105+ ) ;
36106 }
37107 }
38108}
0 commit comments