Question about StreamWriter Class

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
 
G

Greg Burns

I don't see this so much as a question about StreamWriter Class as it is
about UI design.

I see what you mean about it being redundant. I think the answer depends on
personal taste. Here is my swing at it:

My form has a read-only textbox named txtFilename (which contains the
default save filename), a browse button btnBrowse, and a save button
btnSave.

I took the approach that you shouldn't be bothered with a "File already
exists" message when browsing for where to save the file, only when you
click Save. (My reasoning is you want to prompt at least once, the only
gauranteed path is through the Save button).

That ran into the problem if the file exists already and you choose not to
save overtop but then you go and pick another file that also already exists.
I have it prompt in this case.

Good luck
Greg


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
txtFilename.Text = "c:\default.txt"
SaveFileDialog1.OverwritePrompt = False
End Sub

Private Sub Browse()
SaveFileDialog1.FileName = txtFilename.Text

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel
Then Exit Sub

txtFilename.Text = SaveFileDialog1.FileName
End Sub

Private Sub Browse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnBrowse.Click
SaveFileDialog1.OverwritePrompt = False
Browse()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
If System.IO.File.Exists(txtFilename.Text) Then

Dim r As DialogResult = MessageBox.Show(txtFilename.Text & "
already exists?" & vbCrLf & "Do you want to replace it?", "Save As",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If r = Windows.Forms.DialogResult.No Then
SaveFileDialog1.OverwritePrompt = True
Browse()
End If

End If

MsgBox("Saving " & txtFilename.Text)
' do save here...
End Sub


End Class
 
D

David Buchan

Hi Bill,

I've tried out your suggestion and it seems to work.

You're right, this was more of an interface question.

I've set the overwriteprompt to false in my browse subroutine, and added
a query for overwrite in the save subroutine. Thus removing the
redundancy.

In the case in which the file already exists, I ask whether to
overwrite. If they say yes, then streamwriter does its thing. If they
say no, I simply exit the save subroutine, implying they'll need to go
back and either type in a new filename in the textbox, or use the browse
button.

I think that should account for all contingencies. I'll have to think
about it and try to give it various scenarios.

Thanks for your help,
Dave
 

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