473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem with SaveFileDialog

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
Nov 16 '05 #1
6 19256
"Ken Allen" <ke******@sympatico.ca> wrote in message
news:eA**************@TK2MSFTNGP10.phx.gbl...

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");
}

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
Nov 16 '05 #2
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
Nov 16 '05 #3
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;

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
"Ken Allen" <ke******@sympatico.ca> wrote in message
news:eA**************@TK2MSFTNGP10.phx.gbl...

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");
}

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com

Nov 16 '05 #4
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

"Marcin Grzębski" <mg*******@void.taxussi.com.pl.void> wrote in message
news:c6***********@mamut.aster.pl...
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

Nov 16 '05 #5
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
Nov 16 '05 #6
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());
-----------------------------------------------------------------

"Marcin Grzębski" <mg*******@void.taxussi.com.pl.void> wrote in message
news:c6***********@mamut.aster.pl...
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

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: juli | last post by:
Hello dear fellows! Is there any chance that you know how can I upload a file with the saveFileDialog control to a specific folder-> I want any file uploaded to be save on specific folder with...
5
by: Karl | last post by:
Hi, I have some code that will save the contents of a Rich Text Box in either a Text or Rich Text Format file. The code is using the SaveFileDialog and is working correctly. I have been...
3
by: josh | last post by:
How do I make it actually save or open a file? It only opens the dialogs. What do I type to get it to save? Here's what I have so far: Public Class frmMainApp Inherits...
1
by: Rob | last post by:
Hi, I have recently installed Visual Studio 2005 and although I'm starting to understand the 64 bit differences, I seem to have run into another problem. I would like to implement a simple "Save...
2
by: ad | last post by:
Hi, I want to use saveFileDialog to save a file (like c:\aa.zip) to another place. How can I assgin c:\aa.zip to saveFileDialog
8
by: gopal | last post by:
Hi, I would like to display only the file names in SaveFileDialog control when i open it the Window in the without the complete file path Example if the file - -- test.doc is in folder...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
3
by: =?Utf-8?B?UmljaA==?= | last post by:
I want to create a class library project (dll) for use with a com app. The idea is to use .Net objects in com. I also want to include a SaveFileDialog in the class library. Is it possible to...
8
by: Joe Duchtel | last post by:
Hello - I have the following code to detemine a file name when my application is saving a file. The problem is that if the file already exists and I select the Yes button in the "Do you want to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.