This project has moved. For the latest updates, please go here.

Call Save() method recursively

Jun 2, 2014 at 7:09 AM
Hi!

There is existing file with name: app.xlsx. I wold like to save a new xlsx with the same name and path. When the original file is open, then the Save() throws the InvalidOperationException. That is good, but when I catch the exception, then I would like to call the saving again, until the user closes the app.xlsx manually, then after he closed the file, the the program continously throws the exception. It is possible to save this problem with EPPLUS?
Here is my code:
        public Form1()
        {
            InitializeComponent();
        }

        ExcelPackage package = new ExcelPackage();
            
        private void button1_Click(object sender, EventArgs e)
        {
            ExcelWorksheet wv = package.Workbook.Worksheets.Add("Test Page");
            wv.Cells["A1"].Formula = "SUM(A2:A3)";
            wv.Cells["A2"].Value = 2;
            wv.Cells["A3"].Value = 3;

            wv.Cells["A1"].Calculate();

            Save();

            this.Close();
        }

        private void Save()
        {
            try
            {
                package.SaveAs(new FileInfo("app.xlsx"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Save();
            }
        }