Solved

Writing to a text file

Posted on 2006-06-16
7
166 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 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