your code seems completely wrong to me. Here are some of the problems:
- a SaveFileDialog is meant for the user to enter a new filename or choose an existing filename, or to cancel the intended operation. To do that correctly you should compare the return value of ShowDialog() against DialogResult.OK (you compare FileName with empty string, I doubt that is equivalent) and use the FileName value as the path of the output file (you don't use the value at all).
- (the part shown of) your code does not declare, create or use writer and file except for closing it, so at best something gets written somewhere else, and some (which?) file gets closed; anyway changing the state of writer and file should not be a side effect of the
saveDialog.FileName != "" test, the writer and the file operation should only exist inside the code block following that test, and nowhere else.
- declaring a FileStream and opening a file inside the code block of the if statement, while not using that stream, does not make any sense. Once the if-block is done, the stream is out of scope, hence useless.
- if your code creates a FileDialog, it should also dispose of it; the using statement is the easiest way of doing it right.
I suggest you have a look at some CodeProject articles; a lot of them describe small applications that do load and save some data from and to a file, using OpenFileDialog and SaveFileDialog. Just use these as search terms in the CP search facility.
If you don't fully understand the FileDialog class, read its documentation; if you still feel uncomfortable, go buy and study an introductory book on the language of your choice. It will teach you the basics in a systematic way.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 8:38 AM