joneK
joneK

Reputation: 241

How can i check if file is exist ask the user if to destroy it and create the same one again or not?

This is the code:

private void beginOperationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            changeFileNameToolStripMenuItem.Enabled = false;
            if (File.Exists(fullDefaultDirectory))
            {
                File.Delete(fullDefaultDirectory);
            }
            ffmp.Start(fullDefaultDirectory, 25);//"test.avi", @"d:\", 25);
            timer1.Enabled = true;
            startStop = true;
        }

Now i check if the file exist and delete it but thats not good way since i need a file to be on the hard disk.

So what i want to do is:

  1. If the fie exist ask the user if to delete it or run over it with the same name.
  2. If deleted open the savedialog and let the user to set a new file name.
  3. If not deleted just run over the existing file and create the same file name.

This is the savedialog i already have:

private void changeFileNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
             saveFileDialog1.Filter = "Avi|*.avi";
             saveFileDialog1.Title = "Save an Avi File";
             saveFileDialog1.ShowDialog();
             // If the file name is not an empty string open it for saving.
             if (saveFileDialog1.FileName != "")
             {
              //outputFileName = Path.GetFileName(saveFileDialog1.FileName);
              //outputDirectory = Path.GetDirectoryName(saveFileDialog1.FileName);
              fullDefaultDirectory = saveFileDialog1.FileName;
              Options_DB.Set_Video_File(fullDefaultDirectory);
             }
        }

But im talking about a situation when the user didnt change anything and the variable fullDefaultDirectory contain the same directory and file name then let the user to decide if to delete or run over it .

fullDefaultDirectory always contain a file name since i have a settings file name text file where i save the directory and file name the user selected. If he didnt select anything the default is some file i did to be default and if he selected a file it will create the file the user selected.

I need to solve the case where the file is already exist.

Upvotes: 1

Views: 172

Answers (1)

Dr Herbie
Dr Herbie

Reputation: 3940

The SaveFileDialog class already covers this with the OverwritePrompt property, which will cause the dialog to ask the user if they're sure they want to overwrite an existing file.

Upvotes: 1

Related Questions