Link to home
Start Free TrialLog in
Avatar of AndrewMagruder
AndrewMagruder

asked on

I need a StreamWriter in a .Net Windows Application available to all forms.

I want to open a new text file for writing when my VB .NET 2005 Windows application starts and have all Forms in the application able to write to it.  I want the file to close when the ap closes. i've tried code in MAIN and MyApplication Startup event handler but the streamwriter is nothing in the forms.
Imports Microsoft.VisualBasic.ApplicationServices
Imports System.IO
 
 
Namespace My
 
    ' The following events are availble for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
 
 
        Public Debug_Flow_WRITE As StreamWriter
        Public Property Debug_Flow_SR() As StreamWriter
            Get
                Return Debug_Flow_WRITE
            End Get
            Set(ByVal value As StreamWriter)
                Debug_Flow_WRITE = value
            End Set
        End Property
 
 
        Public Sub Me_Startup( _
        ByVal sender As Object, _
        ByVal e As StartupEventArgs _
        ) Handles Me.Startup
 
            File.Delete("C:\Program Files\KVH Industries Inc\M10_Scan 1.1\M10_Scan_Trace.txt")
            Debug_Flow_WRITE = File.CreateText("C:\Program Files\KVH Industries Inc\M10_Scan 1.1\M10_Scan_Trace.txt")
 
        End Sub
 
        Public Sub Me_ShutDown( _
        ByVal sender As Object, _
        ByVal e As EventArgs _
        ) Handles Me.Shutdown
 
            Debug_Flow_WRITE.Close()
 
        End Sub
 
 
    End Class
 
End Namespace
 
 
 
        ' This code is in a form
        ' Debug_Flow_WRITE is nothing
        ' My.Application.Debug_Flow_SR is nothing too
        My.Application.Debug_Flow_WRITE.WriteLine("Leaving txtKVHSerialNumber.KeyUp at End Sub.")

Open in new window

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Why don't you use

My.Computer.FileSystem.WriteAllText(fileName, Text, True)
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, if you want to be available and more easy in all project, you can build a shared class like this:
Public Class Debug_Flow
 
    Const fileName = "C:\Program Files\KVH Industries Inc\M10_Scan 1.1\M10_Scan_Trace.txt"
 
    Public Shared Sub WRITE(ByVal txt As String)
        My.Computer.FileSystem.WriteAllText(fileName, txt, True)
    End Sub
 
    Public Shared Sub DELETE()
        IO.File.Delete(fileName)
    End Sub
 
End Class
 
 
 
' Then you can use
Debug_Flow.WRITE("bla bla bla")
 
' Or
Debug_Flow.DELETE

Open in new window

Avatar of AndrewMagruder
AndrewMagruder

ASKER

I used VBRocks' code and it worked perfectly.

The other proposed solutions would have worked if I only wrote to the file once. I needed to append lines to the file in dozens of places so WriteAllText would not work.  if I understand correctly, WriteAllText overwrites the file each time it's called.

Thank all of you for your help, it's much appreciated.