By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,033 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,033 IT Pros & Developers. It's quick & easy.

Text Files

P: n/a
I'm trying to create a text file, write to it, close it, then reopen the
same file for reading (line by line). I've so far been able to successfully
open the file, write to it, then close it. I let the user decide which file
to write to, using the following code:
' File writing/reading variables

Dim SaveFileResult As SaveFileDialog

Dim TextFileStream As FileStream

Dim TextStreamWriter As StreamWriter

Dim TextStreamReader As StreamReader

SaveFileResult = New SaveFileDialog

SaveFileResult.Filter = "Text Files|*.txt|All Files|*.*"

SaveFileResult.FilterIndex = 0

Dim FileResult As DialogResult

FileResult = SaveFileResult.ShowDialog

If FileResult = DialogResult.OK Then

' Open file for writing

TextFileStream = SaveFileResult.OpenFile

TextStreamWriter = New StreamWriter(TextFileStream)

Else ' Since they cancelled, exit sub

txtText.Focus()

txtText.Select(0, txtText.Text.Length)

Exit Sub

End If

End If

.... This works successfully. Is there a way I can then change it from
write-only access to read-only access, or would I need to close it first?
Also, how can I open it with the same filename? I'm really new to working
with files, so any help would be appreciated. Thank you very much!
Feb 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Eric,

The samples at MSDN about the streamwriter are in my idea a lot different
what you use, while for your question in my idea they are given.

http://msdn.microsoft.com/library/de...classtopic.asp

You write about the close, however I see it nowhere in your code while it is
really two times needed.

As extra if you want a quick answer, than please make the code about the
problem and not about the OpenFileDialog and the SaveFileDialog as it is
now. Try than to avoid blank lines in your messages, this you can get using
the nobebook or using this program from Armin.

http://www.vb-tips.com/default.aspx?...9-0487108f3513

I hope this helps,

Cor
Feb 20 '06 #2

P: n/a
Hi Eric,

The way I open files using a OpenFileDialog is:

1. Store the path to the file using the FileName property of the FileDialog.
2. Use one of the overloaded constructors of the FileStream class to create
a new filestream:
Dim TextFileStream As New FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.None)

The FileAccess and FileShare enumerations will help you to set Readonly
or Writeonly access.
3. Create a StreamReader or Writer which will then read or write to your
selected file.

You can open the file with the same file name since the path to the file
would be stored in your variable.
Hope that helps,

Regards,

Cerebrus.
"Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
news:HI******************@newssvr27.news.prodigy.n et...
I'm trying to create a text file, write to it, close it, then reopen the
same file for reading (line by line). I've so far been able to successfully open the file, write to it, then close it. I let the user decide which file to write to, using the following code:
' File writing/reading variables

Dim SaveFileResult As SaveFileDialog

Dim TextFileStream As FileStream

Dim TextStreamWriter As StreamWriter

Dim TextStreamReader As StreamReader

SaveFileResult = New SaveFileDialog

SaveFileResult.Filter = "Text Files|*.txt|All Files|*.*"

SaveFileResult.FilterIndex = 0

Dim FileResult As DialogResult

FileResult = SaveFileResult.ShowDialog

If FileResult = DialogResult.OK Then

' Open file for writing

TextFileStream = SaveFileResult.OpenFile

TextStreamWriter = New StreamWriter(TextFileStream)

Else ' Since they cancelled, exit sub

txtText.Focus()

txtText.Select(0, txtText.Text.Length)

Exit Sub

End If

End If

... This works successfully. Is there a way I can then change it from
write-only access to read-only access, or would I need to close it first?
Also, how can I open it with the same filename? I'm really new to working
with files, so any help would be appreciated. Thank you very much!

Feb 20 '06 #3

P: n/a
Cerebrus.

Does still not work if you don't close the file after writing before you
start to read it.

The filedialog is not the qeustion in my opinion. just a way to find the
path.

Cor
Feb 20 '06 #4

P: n/a
Hi Cor,

I know that, of course, but in that case I must have misunderstood Eric's
question. I assumed that the FileStream would be closed before attempting
another read/write.

The way I saw it, he was creating his FileStream using the
"TextFileStream = SaveFileResult.OpenFile" statement, where SaveFileResult
is the SaveFileDialog.

Using this option, one has to go with the default options and is limited in
flexibility as far as Read/Write access control is concerned.

To quote the Help :

SaveFileDialog.OpenFile Method :
------------------------------------
"For security purposes, this method creates a new file with the selected
name and opens it with read/write permissions. This can cause unintentional
loss of data if you select an existing file to save to. To save data to an
existing file while retaining existing data, use the File class to open the
file using the file name returned in the FileName property."

That was what I wanted to explain in my post.

Regards,

Cerebrus.
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
Cerebrus.

Does still not work if you don't close the file after writing before you
start to read it.

The filedialog is not the qeustion in my opinion. just a way to find the
path.

Cor

Feb 20 '06 #5

P: n/a
Thank you both for your replies! I truly appreciate it a lot. You've been
a terrific help to me! :)

-- Eric

"Cerebrus99" <zo*****@sify.com> wrote in message
news:eu*************@TK2MSFTNGP09.phx.gbl...
Hi Cor,

I know that, of course, but in that case I must have misunderstood Eric's
question. I assumed that the FileStream would be closed before attempting
another read/write.

The way I saw it, he was creating his FileStream using the
"TextFileStream = SaveFileResult.OpenFile" statement, where SaveFileResult
is the SaveFileDialog.

Using this option, one has to go with the default options and is limited
in
flexibility as far as Read/Write access control is concerned.

To quote the Help :

SaveFileDialog.OpenFile Method :
------------------------------------
"For security purposes, this method creates a new file with the selected
name and opens it with read/write permissions. This can cause
unintentional
loss of data if you select an existing file to save to. To save data to an
existing file while retaining existing data, use the File class to open
the
file using the file name returned in the FileName property."

That was what I wanted to explain in my post.

Regards,

Cerebrus.
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
Cerebrus.

Does still not work if you don't close the file after writing before you
start to read it.

The filedialog is not the qeustion in my opinion. just a way to find the
path.

Cor


Feb 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.