Question Writing lines in text file

Ab99

Member
Joined
Dec 19, 2021
Messages
8
Location
Netherlands
Programming Experience
3-5
I want to create an text file and want to write lines in it.
Creating the text file is not the problem.

But I can not write outside the Sub Form Load.
I have many subs were I want to writes lines into the same txt file.

Writing in an text file:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.WindowState = FormWindowState.Maximized
            Timer4.Enabled = False
            comPORT1 = ""
            For Each sp As String In My.Computer.Ports.SerialPortNames
                Compoort1_ComboBox.Items.Add(sp)
            Next

            '###
            Timer10.Enabled = False
            comPORT2 = ""
        For Each sp As String In My.Computer.Ports.SerialPortNames
            Compoort2_ComboBox.Items.Add(sp)
        Next

        Dim Datumtijd As Date = Now()

        fPath = "C:\TimingForHorses\Logging\log001.txt"
        Dim afile As New IO.StreamWriter(fPath, True)
        afile.WriteLine("hello1")
        afile.Close()




    End Sub

How should I do this?
 
This is a simple matter of scope. You create a StreamWriter object and assign it to the local variable aFile. That means that you can only access that object where you can access that variable. If you want to access the object outside that method then you need to assign it to a variable that is accessible outside that method.

That said, you probably shouldn't do that anyway. It's generally not going to be appropriate to open a file and then keep it open for long periods of time that you're not using it. You should generally open the file, use it as required and then close it, opening it again later if you need to use it again. You can just call the File.AppendAllText to write text to the end of a file wherever you like, without need of a StreamWriter object or variable of your own.
 
By the way, please post in the forum most specific to the issue. The VS.NET General forum is for IDE issues, not language issues. I have moved this thread to the VB.NET General forum, which is for language issue.
 
This is a simple matter of scope. You create a StreamWriter object and assign it to the local variable aFile. That means that you can only access that object where you can access that variable. If you want to access the object outside that method then you need to assign it to a variable that is accessible outside that method.

That said, you probably shouldn't do that anyway. It's generally not going to be appropriate to open a file and then keep it open for long periods of time that you're not using it. You should generally open the file, use it as required and then close it, opening it again later if you need to use it again. You can just call the File.AppendAllText to write text to the end of a file wherever you like, without need of a StreamWriter object or variable of your own.
My coal is messages writing to an text file when my programm becomes in an not normal situation.
An example is when the serial connection is not accesble.
In the text file I can see this problems.
When my form3 is open this file should be acceseble for writing to it.
 
My coal is messages writing to an text file when my programm becomes in an not normal situation.
An example is when the serial connection is not accesble.
In the text file I can see this problems.
When my form3 is open this file should be acceseble for writing to it.
Oké, I get it.
I must every time the stream open.
This functions.
Thanks.
 
Back
Top