D
David Buchan
I have written a program to write some text to a file. Currently, if the
file already exists, the program simply overwrites it.
What I'd like to do, is have the program ask me if I wish to overwrite
the existing file, ask for a new name if the answer is no, or overwrite
if the answer is yes.
I have the line:
Dim objWriter As StreamWriter = New StreamWriter(OutputFileName)
then my program goes and writes to the file:
objWriter.WriteLine("blah blah blah")
etc.
and finally:
objWriter.Close()
objWriter = Nothing
Issue 1) I don't know how to get StreamWriter to handle the situation
where the file already exists.
Issue 2) I have a text box on my form that contains the default
filename. If you click on my browse button, I open up the
SaveFileDialog1 method, where the user can find the directory they want
to save to and type in any filename. If the filename already exists, it
asks you to overwrite. If you say no, it'll keep the savedialog box open
so you can type in a new name. If you say yes, it saves that path and
filename into OutputFileName. I'm thinking that I should remove the
check for an already existing file in the savedialog box, and leave that
to the streamwriter call. Otherwise, it's a bit redundant. So,
basically, I'm a bit mixed up as to how to proceed. Note: I'm a total
newbie to vb.net, and event-based programming in particular.
Any help would be appreciated.
Thanks,
Dave
file already exists, the program simply overwrites it.
What I'd like to do, is have the program ask me if I wish to overwrite
the existing file, ask for a new name if the answer is no, or overwrite
if the answer is yes.
I have the line:
Dim objWriter As StreamWriter = New StreamWriter(OutputFileName)
then my program goes and writes to the file:
objWriter.WriteLine("blah blah blah")
etc.
and finally:
objWriter.Close()
objWriter = Nothing
Issue 1) I don't know how to get StreamWriter to handle the situation
where the file already exists.
Issue 2) I have a text box on my form that contains the default
filename. If you click on my browse button, I open up the
SaveFileDialog1 method, where the user can find the directory they want
to save to and type in any filename. If the filename already exists, it
asks you to overwrite. If you say no, it'll keep the savedialog box open
so you can type in a new name. If you say yes, it saves that path and
filename into OutputFileName. I'm thinking that I should remove the
check for an already existing file in the savedialog box, and leave that
to the streamwriter call. Otherwise, it's a bit redundant. So,
basically, I'm a bit mixed up as to how to proceed. Note: I'm a total
newbie to vb.net, and event-based programming in particular.
Any help would be appreciated.
Thanks,
Dave