Пример #1
0
        public void SaveLog(bool force = false)
        {
            string newFile, backupFile, currentFile;

            if (force)
            {
                currentFile = "CommandersLogAutoSave.xml";
            }
            else
            {
                currentFile = "Commander's Log Events to " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".xml";
            }

            newFile    = String.Format("{0}_new{1}", Path.GetFileNameWithoutExtension(currentFile), Path.GetExtension(currentFile));
            backupFile = String.Format("{0}_bak{1}", Path.GetFileNameWithoutExtension(currentFile), Path.GetExtension(currentFile));

            Stream stream = new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.None);
            var    x      = new XmlSerializer(LogEvents.GetType());

            x.Serialize(stream, LogEvents);
            stream.Close();

            // we delete the current file not until the new file is written without errors

            if (force)
            {
                // delete old backup
                if (File.Exists(backupFile))
                {
                    File.Delete(backupFile);
                }

                // rename current file to old backup
                if (File.Exists(currentFile))
                {
                    File.Move(currentFile, backupFile);
                }
            }
            else
            {
                // delete file if exists
                if (File.Exists(currentFile))
                {
                    File.Delete(currentFile);
                }
            }

            // rename new file to current file
            File.Move(newFile, currentFile);
        }