Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,477 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!




Writing Text Files

 
Reply to this topicStart new topic

Writing Text Files

chili5
27 Jan, 2008 - 08:58 AM
Post #1

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 763



Thanked: 4 times
My Contributions
I am making a game where I want to write to variables to text files to be able to store them. I have it set to read the file and what I want to happen is if the variable clicks is greater than the value in the text file "clicks.txt" to replace the text there with the contents of the variable.

CODE


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim scoreReader As IO.StreamReader = IO.File.OpenText("clicks.txt")
        Dim clickCount As Integer = scoreReader.ReadLine
        If totalclicks > clickCount Then
            Dim file As System.IO.StreamWriter
            file = My.Computer.FileSystem.OpenTextFileWriter("clicks.txt", True)
            file.WriteLine(clicks)
        End If
    End Sub



I know how to read files but can't really figure out how to write files. When I run this I get a IO Exception was unhandled message. Saying that the text file is being used but I'm certain that it isn't.

Edit: Also my text files are in the debug folder in bin.

Thanks smile.gif

This post has been edited by chili5: 27 Jan, 2008 - 09:00 AM
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Writing Text Files
27 Jan, 2008 - 10:29 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,478



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You're trying to open a file 2 times, this line opens the file:


CODE

Dim scoreReader As IO.StreamReader = IO.File.OpenText("clicks.txt")



And this line is trying to open a file that is already open:


CODE

file = My.Computer.FileSystem.OpenTextFileWriter("clicks.txt", True)



Thus the System.IOException being thrown as you're trying to first open a file then open and write to a file thats been opened already.
User is online!Profile CardPM
+Quote Post

chili5
RE: Writing Text Files
27 Jan, 2008 - 11:32 AM
Post #3

D.I.C Addict
****

Joined: 28 Dec, 2007
Posts: 763



Thanked: 4 times
My Contributions
So then I don't need to read the file, I just have to open it for writing correct?

Not really sure about writing into the file but what I want to do is read the file and if the number in my totalclicks variable is more than the value in the text file then erase the content in the text file and add the new number.
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: Writing Text Files
27 Jan, 2008 - 06:26 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,478



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
If you are new to using the System.IO Namespace, and writing to files and such, I have written a 2 Part tutorial on the System.IO Namespace that I think might help you a little:


System.IO in VB.Net Part I
System.IO in VB.Net Part II


There will be some good information for you for reading, writing and manipulating text files. Hope it helps.

Happy Coding! smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 02:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month