0

I've looked at other posts on here about this message, but none of them seem to match but scenario, hence the post.

I've trying to save a file (which works fine) but if the file is open but he user, then the exception is thrown (of course).

My question is : How do I save the file to a numerically prefixed name if the selected save as fix is already open?

Or

CATCH the exception to display a "don't be silly, close the file first, then save" message ?

The code I am (currently) using is

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        string filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
        saveFileDialog1.Filter = filter;

        const string header = "CSV HEADER TITLES";
        string LineOneData = Variables;

        StreamWriter writer = null;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filter = saveFileDialog1.FileName;
            using (writer = new StreamWriter(filter))
            {
                writer.WriteLine(header);
                writer.WriteLine(LineOneData);
                writer.Close();
            }
        }
Gerald Oakham
  • 173
  • 12
  • 1
    `... catch(Exception ex) { MessageBox.Show(ex.Message); }` ? – bitbonk Aug 03 '17 at 12:08
  • Hi @bitbonk, I did try an Exception Catch, but it seems to miss it everytime. I could be wrong, but I thought catching this type of exception was a "no no". Shouldn't I try to evaluate the filename being chosen before the exception is thrown ? – Gerald Oakham Aug 03 '17 at 12:12
  • Can you not set the name using saveFileDialog1.FileName = "THIS.CSV" ? – KL_ Aug 03 '17 at 12:14
  • Hi @Yahtzee , I'm letting the user pick the filename, so it could be different each time. in the case that they double click on a already existing file (that is currently open) , then I want to stop the save, and either save to the same filename +1 (ie test1.csv) or display a message (instead of crashing the program) – Gerald Oakham Aug 03 '17 at 12:16
  • Depends on what your program is doing but for a certain scenario in which you're editing the file and then saving it within your program the File open code can say that you're looking to read/write. – Bailey Miller Aug 03 '17 at 12:18
  • Please, consider to take a look at this. It helped me on a very similar problem: https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use – Cataklysim Aug 03 '17 at 12:41

1 Answers1

0

This should give result in the behavior you wanted: just try to catch IOException, due to the file is in use case would throw this. Don't forget to close the writer in the finally block. Also you can throw more exceptions like I showed in case of FileName is null (eventhough this might never happen (?) it shall only show you how you can handle different cases):

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
     try{
        var result = saveFileDialog1.FileName ?? throw new System.IO.IOException("No FileName selected?!");
        writer = new StreamWriter(result);            
        writer.WriteLine(header);
        writer.WriteLine(LineOneData);
        writer.Close();
        }
      catch (System.IO.IOException exp)
        {
          DoSomethingWithThe(exp);
        } 
       finally
        {
          if (writer != null)
            {
                writer.Close();
            }           
        }
    }
Jodn
  • 314
  • 1
  • 11