Esempio n. 1
0
        public void Save()
        {
            JsonSerializer serializer = new JsonSerializer();

            serializer.Formatting = Formatting.Indented;
            var configFile = this;
            LocalAppDataFolder localAppDataFolder = new LocalAppDataFolder();//an object with a method that return a stream to the AppData/local project folder where I save the config file
            FieldInfo          fieldInfo          = this.GetType().GetField("ConfigsFileName", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            string             value = (string)fieldInfo.GetValue(null);

            using (FileStream stream = localAppDataFolder.GetFile(value))
                using (StreamWriter sw = new StreamWriter(stream))
                    using (JsonWriter writer = new JsonTextWriter(sw))
                    {
                        serializer.Serialize(writer, configFile);
                    }
        }
Esempio n. 2
0
        public static T GetConfigs <T>() where T : ConfigsTools, new()
        {
            T result;
            JsonSerializer     serializer         = new JsonSerializer();
            FieldInfo          fieldInfo          = typeof(T).GetField("ConfigsFileName", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            string             value              = (string)fieldInfo.GetValue(null);
            LocalAppDataFolder localAppDataFolder = new LocalAppDataFolder();

            using (FileStream stream = localAppDataFolder.GetFile(value))
                using (StreamReader sw = new StreamReader(stream))
                    using (JsonReader reader = new JsonTextReader(sw))
                    {
                        result = serializer.Deserialize <T>(reader);
                    }
            if (result == null)
            {
                result = new T();
            }
            return(result);
        }