473,503 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text Files

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
5 1603
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
4881
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
0
3286
by: Steve | last post by:
My application uses transfertext to create text files that are FTPed to a website and are processed by a webstore. The text files can be for one of three purposes: Add one or more items to the...
16
2638
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
0
2377
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
3
4248
by: Frustrated Developer via DotNetMonster.com | last post by:
I have posted a couple times on here already and found the user community to be very helpful. I took on a project before I realized how difficult a time I'm having working with a database....
10
3624
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
5
3151
by: praveenholal | last post by:
Hi Freinds , I want to convert the files that are in text format (.txt) to CSV file. I am working on Linux. So can anyone guide me. Here is my problem Description I have some result files...
29
4832
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt,...
1
3413
by: Xah Lee | last post by:
Text Processing with Emacs Lisp Xah Lee, 2007-10-29 This page gives a outline of how to use emacs lisp to do text processing, using a specific real-world problem as example. If you don't know...
0
10706
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.