473,651 Members | 3,068 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 TextStreamWrite r As StreamWriter

Dim TextStreamReade r 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

TextStreamWrite r = New StreamWriter(Te xtFileStream)

Else ' Since they cancelled, exit sub

txtText.Focus()

txtText.Select( 0, txtText.Text.Le ngth)

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 1612
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(strF ileName, 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*****@dontlo okforme.com> wrote in message
news:HI******** **********@news svr27.news.prod igy.net...
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 TextStreamWrite r As StreamWriter

Dim TextStreamReade r 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

TextStreamWrite r = New StreamWriter(Te xtFileStream)

Else ' Since they cancelled, exit sub

txtText.Focus()

txtText.Select( 0, txtText.Text.Le ngth)

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
"TextFileSt ream = 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******** ******@TK2MSFTN GP15.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.c om> wrote in message
news:eu******** *****@TK2MSFTNG P09.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
"TextFileSt ream = 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******** ******@TK2MSFTN GP15.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
4927
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 any solid reasons to prefer text files over binary files files?
0
3311
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 webstore, Update one or more existing items in the webstore or delete one or more items from the webstore. The Add text files begin with ADD in the first field to signal the webstore to add the item, the Update textfiles begin with UPDATE in the...
16
2658
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
2387
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 D 'Delete
3
4262
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. Assistance would be greatly appreciated! I am trying to allow certain users to be able to preview a database's contents without being able to update which I've accomplished. I've created a "Preview" button and set the datagrid to READ ONLY. Now I want...
10
3637
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 binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and displays
5
3159
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 which contains text fields and numeric fields which are seperated by tab and also there is a - between two numbers 10-20 . Another thing is that colons are also present in between in files name :: praveen
29
4855
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, .cpp, .h, .php, .html). How to check a file an find out if the file is binary or text? Thanks for your help.
1
3433
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 elisp, first take a gander at Emacs Lisp Basics. HTML version with links and colors is at: http://xahlee.org/emacs/elisp_text_processing.html
0
10751
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
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 we have to send another system
2
1591
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.