How can I confirm the filename before the SaveFileDialog is closed?

I am using SaveFileDialog and want to evaluate if a file meets certain conditions before allowing it to be saved. If that doesn't fit the criteria, I don't want the SaveFileDialog to close when I click Save. I thought FileOK might work, but the dialog looks like it was already closed by the time this event is fired and I see no way to prevent it from closing anyway.

0


a source to share


3 answers


FileOK

is CancelEventHandler

- you just need to set the Cancel

property CancelEventArgs

to true

.



+3


a source


Try this approach from FileOK handler



private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    //your conditions...
    if (!openFileDialog1.FileName.Equals( "C:\\hello.txt" ) )
    {
        //if fail, set e.cancel
        MessageBox.Show(@"File name must equal c:\hello.txt.");
        e.Cancel = true;

    }            
}

      

+3


a source


0


a source







All Articles