Click here to Skip to main content
15,908,013 members
Home / Discussions / C#
   

C#

 
GeneralRe: Save and Save as Pin
squerley2-Dec-10 12:15
squerley2-Dec-10 12:15 
GeneralRe: Save and Save as Pin
GenJerDan2-Dec-10 17:22
GenJerDan2-Dec-10 17:22 
GeneralRe: Save and Save as Pin
Dalek Dave2-Dec-10 12:27
professionalDalek Dave2-Dec-10 12:27 
AnswerRe: Save and Save as Pin
Luc Pattyn2-Dec-10 12:10
sitebuilderLuc Pattyn2-Dec-10 12:10 
AnswerRe: Save and Save as Pin
_Erik_3-Dec-10 3:22
_Erik_3-Dec-10 3:22 
GeneralRe: Save and Save as Pin
squerley3-Dec-10 8:59
squerley3-Dec-10 8:59 
GeneralRe: Save and Save as Pin
squerley4-Dec-10 16:08
squerley4-Dec-10 16:08 
GeneralRe: Save and Save as Pin
_Erik_6-Dec-10 23:35
_Erik_6-Dec-10 23:35 
Have another look at the methodology I posted in my previous answer. If you use it you will see that on your save_click event handler you only have to call Save method. If the file is new (_filename field is null) the it will call SaveAs method, asking for a file name, and if the file is not new it will save the changes with the same name it had. If you also have a SaveAs_click event handler, you should only call SaveAs method. Anyhow, I can see several mistakes on your code.

First of all, you are creating a new instance of a SaveFileDialog every time you want to save a file, but you do not dispose it at the end of the method. The best here is to add the dialog to the form at design time, dragging and dropping it from the toolbox as if you were adding any other control. This way you will only need one instance of the dialog and it will be disposed when your form gets also disposed. In my previous post this SaveFileDialog was the dlg object I used in the SaveAs method.

Once you call the ShowDialog method, the only one thing you have to check is the DialogResult object it returns. Checking for its FileName property before is a mistake, becouse if it returns a DialogResult.Ok then you can be sure its FileName property is right. In your code you have placed two consecutive if clauses, and if the user selects a filename both of them will evaluate their condition to true, so both of them will execute.

There is another important thing to pay attention. You are not disposing or closing the StreamWriter if there is an exception in the Write method. There are several ways to do this, but this is the one I like most:

C#
try
{
    using (StreamWriter sw = new StreamWriter(filename))
    {
        // Perform here the write operations
        // If there is an exception here the StreamWriter objet will be disposed
    }
}
catch (Exception ex)
{
    // Handle the exception here: notify user, log, ...
}


As I said, have another look at my previous post and make some tests with it. Once you completely understand it you will see that it is all you need and you can apply the same logic to many different applications.
GeneralRe: Save and Save as Pin
squerley10-Dec-10 15:46
squerley10-Dec-10 15:46 
QuestionHow to delete word field code from C# Pin
Andraw Tang2-Dec-10 8:45
Andraw Tang2-Dec-10 8:45 
QuestionXPathNavigator iterate and write Pin
Mark F.2-Dec-10 5:38
Mark F.2-Dec-10 5:38 
AnswerRe: XPathNavigator iterate and write Pin
cechode2-Dec-10 13:15
cechode2-Dec-10 13:15 
GeneralRe: XPathNavigator iterate and write [modified] Pin
Mark F.3-Dec-10 9:00
Mark F.3-Dec-10 9:00 
AnswerRe: XPathNavigator iterate and write Pin
cechode3-Dec-10 9:34
cechode3-Dec-10 9:34 
QuestionAvoid Peer 2 Peer acces Pin
utunity2-Dec-10 5:08
utunity2-Dec-10 5:08 
AnswerRe: Avoid Peer 2 Peer acces Pin
musefan2-Dec-10 5:36
musefan2-Dec-10 5:36 
GeneralRe: Avoid Peer 2 Peer acces Pin
utunity2-Dec-10 5:45
utunity2-Dec-10 5:45 
GeneralRe: Avoid Peer 2 Peer acces Pin
musefan2-Dec-10 5:51
musefan2-Dec-10 5:51 
GeneralRe: Avoid Peer 2 Peer acces Pin
Luc Pattyn2-Dec-10 7:48
sitebuilderLuc Pattyn2-Dec-10 7:48 
GeneralRe: Avoid Peer 2 Peer acces Pin
fjdiewornncalwe3-Dec-10 1:15
professionalfjdiewornncalwe3-Dec-10 1:15 
AnswerRe: Avoid Peer 2 Peer acces Pin
T M Gray2-Dec-10 11:08
T M Gray2-Dec-10 11:08 
QuestionFlickering Pin
peropata2-Dec-10 3:43
peropata2-Dec-10 3:43 
AnswerRe: Flickering Pin
musefan2-Dec-10 3:52
musefan2-Dec-10 3:52 
AnswerRe: Flickering Pin
phil.o2-Dec-10 4:07
professionalphil.o2-Dec-10 4:07 
GeneralRe: Flickering Pin
musefan2-Dec-10 4:11
musefan2-Dec-10 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.