-2

I'm trying to write a variable in a txt file, but it's not really working. If I replace the variable with just a normal string it's fine. Code:

    Private Sub write()

    Dim writer As System.IO.StreamWriter
    writer = My.Computer.FileSystem.OpenTextFileWriter("c:\TTTemp\Dat.txt", True)

    Dim count As Integer
    Dim chcount As Integer = 11
    Dim readchar As Char
    Dim files As String
    Dim ids(63) As String

    For Each foundFile In My.Computer.FileSystem.GetFiles(
    "C:\TTTemp")
        files = foundFile
        chcount = 11
        For i = 1 To 14
            readchar = GetChar(files, chcount)
            ids(count) = ids(count) & readchar
            chcount += 1
        Next
        writer.WriteLine(ids(count))
        count += 1
    Next

    writer.Close()

End Sub
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dave12311
  • 29
  • 5

3 Answers3

1

Are you running this sub from the load event handler of a form? If so and the code throws an exception the form will still load without anything written to the file. One way to test this is run the sub from a button click event handler instead.

One possible area for an exception to be thrown is not checking the length of the string before you try and read characters from it.

You really should take a look the System.IO class and the Substring method of the String class to find more efficient ways of doing this.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
0

A common reason if none or not all contents are write to file is that you don't flush the written content. Also, you should dispose the writer reliably by adding a Using statement (you can omit the call to Close then:

Private Sub write()

    Using writer As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("c:\TTTemp\Dat.txt", True)
        ' ...
        writer.Flush()
    End Using

End Sub
Markus
  • 20,838
  • 4
  • 31
  • 55
0

First line of you code should have:

Imports System.IO

which controls "StreamWriter" actions..

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dutch Glory
  • 23,373
  • 1
  • 17
  • 6