Solved

Writing to a text file

Posted on 2006-06-16
7
164 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now