/// <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 e)
                {
                    e.ReportException();
                    // ignore silently
                }
            }

            bool reset = info.ResetIfMonthChanged();

            if (!fileExists || reset)
            {
                info.JsonSerialize(installInfoPath);
            }
            return(info);
        }
Beispiel #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);
        }
Beispiel #3
0
        private static void WriteToDisk(string installInfoPath, InstallationInfo info)
        {
            if (WriteToDiskOverride != null)
            {
                WriteToDiskOverride(installInfoPath, info);
                return;
            }

            try
            {
                var json = JsonConvert.SerializeObject(info, Formatting.Indented);
                File.WriteAllText(installInfoPath, json, Encoding.UTF8);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                // ignore silently without sending to telemetry (not yet initialized)
            }
        }