Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to deserialize a InstallationInfo object from the given directory path,
        /// returns a newly constructed InstallationInfo object otherwise
        ///
        /// Resets/reserializes the InstallationInfo object if needed
        /// </summary>
        /// <param name="path">directory path from which to load the json</param>
        /// <returns></returns>
        public static InstallationInfo LoadFromPath(string path)
        {
            var installInfoPath         = Path.Combine(path, InstallationInfo.InstallationInfoFileName);
            InstallationInfo info       = new InstallationInfo();
            bool             fileExists = File.Exists(installInfoPath);

            if (fileExists)
            {
                var text = File.ReadAllText(installInfoPath, Encoding.UTF8);
                try
                {
                    info = JsonConvert.DeserializeObject <InstallationInfo>(text);
                }
                catch (JsonException)
                {
                    // ignore silently
                }
            }

            bool reset = info.ResetIfMonthChanged();

            if (!fileExists || reset)
            {
                info.JsonSerialize(installInfoPath);
            }
            return(info);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to deserialize an InstallationInfo object from the given directory path,
        /// returns a newly constructed InstallationInfo object otherwise
        ///
        /// Resets/reserializes the InstallationInfo object if needed
        /// </summary>
        /// <param name="path">directory path from which to load the json</param>
        /// <param name="utcNow">The current UTC time</param>
        /// <returns>The current InstallationInfo object</returns>
        public static InstallationInfo LoadFromPath(string path, DateTime utcNow)
        {
            var installInfoPath   = Path.Combine(path, InstallationInfoFileName);
            InstallationInfo info = ReadFromDisk(installInfoPath);

            if (info.ResetIfMonthChanged(utcNow))
            {
                WriteToDisk(installInfoPath, info);
            }
            return(info);
        }