コード例 #1
0
        /// <summary>
        /// Saves a ProgramContext to disk as xml.
        /// </summary>
        /// <param name="context">The context to save.</param>
        /// <param name="filepath">The path, name, and extension to save the context.</param>
        /// <returns>True if the save was successful.</returns>
        public static bool Save(ProgramContext context, string filepath)
        {
            string fileDirectory = Path.GetDirectoryName(filepath);

            if (context == null)
            {
                Program.WriteLine(string.Format("Cannot save null xml file."), Format.Error);
                return(false);
            }
            else if (!Directory.Exists(fileDirectory))
            {
                Program.WriteLine(string.Format("Cannot save xml file {0} to {1}. The directory does not exist.", context, fileDirectory), Format.Error);
                return(false);
            }
            else if (File.Exists(filepath))
            {
                Program.WriteLine(string.Format("Cannot create xml file. {0} because it already exists.", filepath), Format.Error);
                return(false);
            }
            else
            {
                try
                {
                    var serializer = new XmlSerializer(typeof(ProgramContext));
                    using (TextWriter writer = new StreamWriter(filepath))
                    {
                        serializer.Serialize(writer, context);
                        Program.WriteLine(string.Format("Saved a settings file to '{0}' for '{1}'.", filepath, context), Format.Notify);
                        return(true);
                    }
                }
                catch (Exception exception)
                {
                    if (File.Exists(filepath))
                    {
                        Program.WriteLine(string.Format("Deleting the temporary xml file {0}", filepath), Format.Notify);
                        File.Delete(filepath);
                    }

                    Program.WriteLine(exception.ToString(), Format.Error);
                    return(false);
                }
            }
        }
コード例 #2
0
 public abstract bool Run(ProgramContext context);