0

How to keep the savefilediallog open when you write to a file which is in use by an other program so that you can change the file name and try to save again?

private void button1_Click_2(object sender, EventArgs e)
{
    Cursor.Current = Cursors.WaitCursor;
    CsvExport = Class_ExportData.DataTableToCSV(datatabelControle, csvSCheidingteken);
    Cursor.Current = Cursors.Default;

    saveFileDialog1.OverwritePrompt = true;

    saveFileDialog1.Filter = "Komma gescheiden waarden (*.csv)|*.csv|Tekst bestanden (*.txt)|*.txt|Alle formaten (*.*)|*.*";
    saveFileDialog1.DefaultExt = "csv";
    saveFileDialog1.AddExtension = true;
    saveFileDialog1.ShowDialog(); 
}

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    try
    {
        string name = saveFileDialog1.FileName; // Get file name.        
        File.WriteAllText(name, CsvExport);     // Write to the file name selected.
    }
    catch (Exception ex)
    {
        //file is locked, how to get back to the open save file dialog ???
    }
}
Hansvb
  • 113
  • 1
  • 1
  • 13
  • Maybe [this](http://stackoverflow.com/questions/3846646/exception-in-opening-a-file-that-is-already-open) can help you. – Luud van Keulen Feb 14 '17 at 20:36
  • You shouldn't do that. You could put your user in a loop where the only way out is to cancel or pick a different name, and it could become rather cumbersome to alert them to that efficiently without creating lots of mouse clicks... – Dan Field Feb 14 '17 at 20:43
  • 2
    the established UX pattern for that would be to give feedback to the user first, that the file is locked for writing (ideally: by whom/which process). That message would provide options to cancel or retry, and on retry you would open the savefiledialog again. If you reuse the same instance, it would preserve the path and file name last selected so it is less of a hassle for the user. – Cee McSharpface Feb 14 '17 at 20:47
  • Ask user if they want to force close the file? Then obtain the file object and close it for them. Just a thought.. possibly not a good idea. – aguertin Feb 14 '17 at 20:53

1 Answers1

1

Try this. Move the code associated with opening the saveFileDialog1 into its own function and invoke that function from button1_Click:

private void button1_Click_2(object sender, EventArgs e)
{
    Cursor.Current = Cursors.WaitCursor;
    CsvExport = Class_ExportData.DataTableToCSV(datatabelControle, csvSCheidingteken);
    Cursor.Current = Cursors.Default;

    ShowSaveFileDialog(); 
}

private void ShowSaveFileDialog()
{
    saveFileDialog1.OverwritePrompt = true;

    saveFileDialog1.Filter = "Komma gescheiden waarden (*.csv)|*.csv|Tekst bestanden (*.txt)|*.txt|Alle formaten (*.*)|*.*";
    saveFileDialog1.DefaultExt = "csv";
    saveFileDialog1.AddExtension = true;
    saveFileDialog1.ShowDialog();
}

EDIT: On further consideration, I don't think you want/need the loop here, so I've removed it. You still want to invoke the ShowSaveFileDialog method here in case of exceptions, though:

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    try
    {
        string name = saveFileDialog1.FileName; // Get file name.        
        File.WriteAllText(name, CsvExport);     // Write to the file name selected.
        return;
    }
    catch (Exception ex)
    {
        //file is locked, how to get back to the open save file dialog ???
        // maybe display an error message here so that the user knows why they're about to see the dialog again.
    }
    ShowSaveFileDialog();
}

Technically, this can probably lead to a StackOverflowException if the user tries repeatedly (and I mean thousands of times) to retry the save after an exception, but that's pretty unlikely.

wablab
  • 1,703
  • 13
  • 15