Exemplo n.º 1
0
        public static void Save(LibertyV window)
        {
            if (MessageBox.Show("Are you sure you want to save all changes?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }
            string originalFilename = window.File.Filename;
            String filePath         = window.File.FilePath;

            // Write to temporary file
            string     tempFilePath = null;
            FileStream file         = null;

            if (filePath != null)
            {
                tempFilePath = filePath + "." + Path.GetRandomFileName();
                file         = System.IO.File.Create(tempFilePath);
            }
            else
            {
                // This is a temporary file, we need to open it with the right flags
                tempFilePath = window.TempOutputFile;
                file         = new FileStream(tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete);
            }

            ProgressWindow progress = new ProgressWindow("Saving", update => window.File.Write(file, update), true);

            try
            {
                progress.Run();
            }
            catch (OperationCanceledException)
            {
                // This operation cancelled
                if (filePath != null)
                {
                    // delete the file
                    file.Close();
                    System.IO.File.Delete(tempFilePath);
                }
                else
                {
                    // Well it is a temporary file, so we don't want to delete it, just make it empty again
                    file.SetLength(0);
                    file.Close();
                }
                MessageBox.Show("Operation canceled.");
                return;
            }

            file.SetLength(file.Position);

            if (filePath != null)
            {
                // Update the file and reopen it
                file.Close();
                window.CloseRPF(false);
                System.IO.File.Delete(filePath);
                System.IO.File.Move(tempFilePath, filePath);

                // Now load the file
                file = System.IO.File.Open(filePath, FileMode.Open);
                window.LoadRPF(new RPF7File(file, originalFilename, filePath), true);
            }
            else
            {
                // Notice: We are not going to use the file that we just wrote, because if we are going to write it again, we will read and write from the same file
                // We waste little bit resources right now (because we doesn't release the old resource because we aren't using the new one), but it isn't so bad
                file.Close();
            }
        }
Exemplo n.º 2
0
 public static void Close(LibertyV window)
 {
     window.CloseRPF();
 }