private void SaveAsMenu_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "HOG Files|*.HOG";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int err = 0;
                try
                {
                    datafile.Write(saveFileDialog1.FileName);
                }
                catch (Exception exc)
                {
                    err = FileUtilities.GetErrorCode(exc);
                }

                if (err != 0)
                {
                    host.AppendConsole(FileUtilities.FileErrorCodeHandler(err, "write", "HOG file"));
                }
                else
                {
                    this.Text = string.Format("{0} - Hog Editor", datafile.Filename);
                }
            }
        }
Beispiel #2
0
        //---------------------------------------------------------------------
        // BASIC MENU FUNCTIONS
        //---------------------------------------------------------------------

        /// <summary>
        /// Helper function to save a HAM file, with lots of dumb error handling that doesn't work probably.
        /// </summary>
        /// <param name="filename">The filename to save the file to.</param>
        private void SaveHAMFile(string filename)
        {
            //Get rid of any old backups
            try
            {
                File.Delete(Path.ChangeExtension(filename, "BAK"));
            }
            catch (FileNotFoundException) { }
            catch (DirectoryNotFoundException) { } //Discover this with our face to avoid a 1/1000000 race condition
            catch (UnauthorizedAccessException exc)
            {
                host.AppendConsole(String.Format("Cannot delete old backup file {0}: Permission denied.\r\nMsg: {1}\r\n", Path.ChangeExtension(filename, "BAK"), exc.Message));
            }
            catch (IOException exc)
            {
                host.AppendConsole(String.Format("Cannot delete old backup file {0}: IO error occurred.\r\nMsg: {1}\r\n", Path.ChangeExtension(filename, "BAK"), exc.Message));
            }
            //Move the current file into the backup slot
            try
            {
                File.Move(filename, Path.ChangeExtension(filename, "BAK"));
            }
            catch (FileNotFoundException) { }
            catch (DirectoryNotFoundException) { } //Discover this with our face to avoid a 1/1000000 race condition
            catch (UnauthorizedAccessException exc)
            {
                host.AppendConsole(String.Format("Cannot move old HAM file {0}: Permission denied.\r\nMsg: {1}\r\n", filename, exc.Message));
            }
            catch (IOException exc)
            {
                host.AppendConsole(String.Format("Cannot move old HAM file {0}: IO error occurred.\r\nMsg: {1}\r\n", filename, exc.Message));
            }
            //Finally write the new file
            FileStream stream;

            try
            {
                stream = File.Open(filename, FileMode.Create);
                datafile.Write(stream);
                stream.Close();
                stream.Dispose();
            }
            catch (Exception exc)
            {
                FileUtilities.FileExceptionHandler(exc, "HAM file");
            }
        }