Exemple #1
0
        /// <summary>
        /// Returns the deserialized output of the provided path. Logs and returns default(T) for
        /// failures.
        /// </summary>
        /// <typeparam name="T">JSON deserializes to this type.</typeparam>
        /// <param name="path">The path to save to.</param>
        /// <param name="throwErrors">
        /// If true, exceptions are rethrown. Otherwise, an error message will explain each error.
        /// Errors are not logged if exceptions are rethrown.
        /// </param>
        public static T LoadJson <T>(string path, bool throwErrors = false)
        {
            if (!File.Exists(path))
            {
                string exception = $"The file could not be loaded since \"{path}\" is not a valid directory.";

                if (throwErrors)
                {
                    throw new FileNotFoundException(exception);
                }

                LogUtilities.Log(exception);
                return(default);
Exemple #2
0
        /// <summary>
        /// Returns the <see cref="FileStream"/> associated with the provided file. Displays an
        /// error to the user or throws an exception, based on <paramref name="silentMode"/>.
        /// </summary>
        /// <param name="path">The full path to the file.</param>
        /// <param name="silentMode">
        /// If true, exceptions are re-raised after being logged. Otherwise, the user is presented
        /// with an error and the exception is caught.
        /// </param>
        public static FileStream OpenFile(string path, FileMode mode, bool silentMode = false)
        {
            try
            {
                return(new FileStream(path, mode));
            }
            catch (Exception ex)
            {
                string fileName = Path.GetFileName(path);
                string message  = $"The file \"{fileName}\" could not be loaded.";
                LogUtilities.Log(ex, message);

                if (silentMode)
                {
                    throw;
                }
            }

            return(null);
        }