Problem with SaveFileDialog

K

Ken Allen

I have enocuntered something wierd.

If I invoke an instance of the above dialog and elect to save the file in a
directory where a file of that name does not exist, then the return is
DialogResult.OK, but if I elect to save the file on top of a file with the
same name in the same directory, when I press the Save button I get a "do
you want to overwrite" dialog -- even if I press OK the SaveFileDialog
returns DialogResult.Cancel!

How can I detect that the user actually pressed Save and elected to
overwrite an existing file?

-Ken
 
M

Mickey Williams

How can I detect that the user actually pressed Save and elected to
overwrite an existing file?

That's a common scenario - it works for most people... How does your code
differ from this:

SaveFileDialog sfd = new SaveFileDialog();
if(sfd.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("ok");
}
else
{
MessageBox.Show("nok");
}
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Ken,
I have enocuntered something wierd.

If I invoke an instance of the above dialog and elect to save the file in a
directory where a file of that name does not exist, then the return is
DialogResult.OK, but if I elect to save the file on top of a file with the
same name in the same directory, when I press the Save button I get a "do
you want to overwrite" dialog -- even if I press OK the SaveFileDialog
returns DialogResult.Cancel!

I think that your problem is that you want to do forbiden operation
is PC file system. As i understand you want to create file with
the same name like existing directory. If I'm wrong then send
feedback.

From my practise: SaveFileDialog does not support to get directory
as a "FileName". So i think that it detects FileSystemObject and then
ask You to override... then if you confirm then SaveFileDialog
understand that you can't create file .... and returns "Cancel".
How can I detect that the user actually pressed Save and elected to
overwrite an existing file?

You can set "OverwritePrompt" to false and then handle the "FileOk"
event. In that handle You can easily check file existence with
File.Exist(...) method.

HTH & Regards

Marcin
 
K

Ken Allen

This is the entire code:

String result = null;

//

// Displays a SaveFileDialog for the specified report.

//

SaveFileDialog saveAs = new SaveFileDialog();

saveAs.FileName = ".\\" + keReportType.ToString() + ".txt";

saveAs.Filter = "Text file|*.txt";

saveAs.Title = "Save a " + keReportType.ToString() + " Report [" +
keReportFormat.ToString() + "]";

if (saveAs.ShowDialog() == DialogResult.OK)

{

result = saveAs.FileName;

}

return result;
 
K

Ken Allen

No, I am attempting to indicate a file name and I get this when the file
already exists -- the save dialog pops up another dialog asking if I wish to
overwrite the file and the return back to my code is "Cancel". I am not
selecting a directory at all.

-Ken
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Ken
No, I am attempting to indicate a file name and I get this when the file
already exists -- the save dialog pops up another dialog asking if I wish to
overwrite the file and the return back to my code is "Cancel". I am not
selecting a directory at all.

....then i don't know what is going on???
Did You try to disable OverridePrompt and check File.Exist(...)
in "FileOk" event handler?
That event handler parameter "e", has Cancel property ... so You can
check it in debug mode.

Not any other solution comes to my mind.

Regards

Marcin
 
K

Ken Allen

Below is the final code that I am using now, and this seems to work OK.

If I set either the CreatePrompt or the OverwritePrompt to true, then if the
sub-dialog is displayed, pressing OK return Cabcel as the dialogState value
in this code! Only if I tell the dialog to quietly overwrite files do I get
back the OK result!

-Ken
-----------------------------------------------------------------
String result = null;
//
// Displays a SaveFileDialog for the specified report.
//
SaveFileDialog saveAs = new SaveFileDialog();
saveAs.CreatePrompt = false;
saveAs.OverwritePrompt = false;
saveAs.FileName = @".\" + keReportType.ToString() + ".txt";
saveAs.DefaultExt = "txt";
saveAs.Filter = "Text file|*.txt";
saveAs.InitialDirectory = @".\";
saveAs.Title = "Save a " + keReportType.ToString() + " Report [" +
keReportFormat.ToString() + "]";
DialogResult dialogState = saveAs.ShowDialog();
if (dialogState == DialogResult.OK)
{
result = saveAs.FileName;
}
MessageBox.Show("Result: " + dialogState.ToString());
-----------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top