3 Replies - 534 Views - Last Post: 25 June 2011 - 03:09 PM Rate Topic: -----

#1 ericzmeh  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 13-September 09

Reading/Writing to Files

Posted 23 June 2011 - 09:30 PM

Im having trouble creating and then writing to a file. First, I check to see if the file exists, if it does not, then I create the file, close it, and then open again for appending text. I am getting an error during debugging saying the file is in use by another process, which is confusing since I am closing the file before opening it to append any further text. Here is my code:

Dim strGroup As String = InputBox("Enter a New Group Name For Selected Friends")
        Dim strArray() As String
        If Not strGroup.Equals("") Then
            If File.Exists(strGroup & ".dat") Then
                MsgBox("This Group Already Exists, Please Add Friends To Existing Group")
            Else
                objWriter = File.CreateText(strGroup & ".dat")
                objWriter.Close()
                For i As Integer = 0 To lbFriends.Items.Count - 1
                    If lbFriends.GetSelected(i) = True Then
                        objWriter = File.AppendText(strGroup & ".dat")
                        InFile = File.OpenText(strInFile)
                        While InFile.Peek >= 0
                            strArray = InFile.ReadLine.Split(" ")
                            If strArray(0) = lbFriends.Items.Item(i) Then
                                objWriter.WriteLine(strArray(0) & "," & strArray(1) & "," & strArray(2))
                                objWriter.Close()
                                InFile.Close()
                                Exit While
                            End If
                        End While
                    End If
                Next
            End If
        End If


Some of these variables are not declared within this method, but they are declared within the class already.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading/Writing to Files

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: Reading/Writing to Files

Posted 24 June 2011 - 06:17 AM

I suggest you use Streamwriter/StreamReader it's far simpler, and you won't have so much difficulty with creating, and writing to a file. :)

Also makes reading line by line simpler in my opinion, as well as the rest of this deal.

http://msdn.microsof...v=vs.71%29.aspx
Was This Post Helpful? 2
  • +
  • -

#3 ericzmeh  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 13-September 09

Re: Reading/Writing to Files

Posted 25 June 2011 - 11:59 AM

Sorry, forgot to mention that objWriter is a declared StreamWriter object and InFile is a declared StreamReader object. Turns out I just had a few statements in the wrong spot, but all cleared up! Thanks for the reply Trevster.
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: Reading/Writing to Files

Posted 25 June 2011 - 03:09 PM

View Postericzmeh, on 25 June 2011 - 11:59 AM, said:

Sorry, forgot to mention that objWriter is a declared StreamWriter object and InFile is a declared StreamReader object. Turns out I just had a few statements in the wrong spot, but all cleared up! Thanks for the reply Trevster.


Dim strmR as system.io.StreamWriter = new System.io.StreamWriter(*PATH* & strGroup & ".dat")



No need for using File. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1