Solved

Writing to a text file

Posted on 2006-06-16
7
167 Views
Last Modified: 2010-04-30
This might be an easy question but I'm having problems writing to a text file in visual basic .net 2005.  The old visual basic syntax of " Open "file" for output as #1" does not seem to work at all.  This is not really the problem though, my problem lies in just getting the syntax right to write to a simple text file created using:

Dim result As System.IO.TextWriter = System.IO.File.CreateText("C:\Documents and Settings\astairs\Desktop\result.txt")

Any help is greatly appreciated!!
0
Comment
Question by:Optamis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
List244 earned 500 total points
ID: 16920715
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21007855.html?query=vb.net+file+write&clearTAFilter=true

Dim file As New System.IO.StreamWriter("c:\test.txt", False) ' if the file exist and Append is False, The file is overwritten
file.WriteLine(content)
file.Close()
0
 

Author Comment

by:Optamis
ID: 16921048
I guess perhaps I should search before I haul off and ask a question...  Very helpful though, fixed my problem 100%.  I do have one more question though, what's the syntax for starting a new line and new page when writing to a text file?
0
 
LVL 8

Expert Comment

by:List244
ID: 16921122
I believe the following would all give you results for new line:

Chr(13) + Chr(10)
file.WriteLine()
ChrW(13) + ChrW(10)
Environment.NewLine()

I do not know exactly what you mean by a new page though.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:List244
ID: 16921127
Also, in the future, you may be better off posting your questions here:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/
0
 

Author Comment

by:Optamis
ID: 16921176
Thanks List244,  Unfortunatly

Chr(13) + Chr(10)
file.WriteLine()
ChrW(13) + ChrW(10)
Environment.NewLine()

isn't working for me to get a new line.  All I need to do is after  the Writeline is start a newline.  Also, what I mean but a new page is that after so many writes or when the page is full it will insert the page number and start a new page.  Any ideas?

Thanks again!!
0
 

Author Comment

by:Optamis
ID: 16921303
I figured it all out, thanks for the help.  Without your guidance I wouldn't have the working writelines.  Thanks List244.
0
 
LVL 8

Expert Comment

by:List244
ID: 16921399
Optamis, I should have been more clear with my first post, what I meant was you could use the chr and environment options
as string appends to add new lines.  The file.WriteLine() would write a line byitself.  They were just many options on how
you could do it.

However, I am glad you got it all worked out.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question