How to automatically write behind the scenes a text file?

The following below is is using the file dialog control in vb. I am wanting to know how to automate a file write behind the scenes.

Const FileDirectory as string
Const File Name as string.

 If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim stream As FileStream = New FileStream(Me.SaveFileDialog1.FileName, FileMode.Create)
                Dim streamwriter As New StreamWriter(stream)
                SaveAs = Me.SaveFileDialog1.FileName
                'Add in Saved file header Information first
                streamwriter.WriteLine(Now)
                streamwriter.WriteLine("Collected Data from G-WHIZ Distance Curve")
                streamwriter.WriteLine("Rate:  " & RateVar)
                streamwriter.WriteLine("Run Distance:  " & DistanceVar)
                For intloop = 0 To DisData.Count - 1
                    System.Threading.Thread.Sleep(100)
                    streamwriter.WriteLine(DisData(intloop))
                    If value = 100 Then
                    Else
                        Application.DoEvents()
                        value = value + 0.5
                        PrgGraphBar.Value = value
                    End If
                Next
                streamwriter.Close()
                stream.Close()
                PrgGraphBar.Value = 0
            End If

Open in new window

cmdolcetAsked:
Who is Participating?
 
NorieConnect With a Mentor VBA ExpertCommented:
So every time this code is executed the number increments?

Perhaps something like this then.
 Const FileDirectory As String = "C:\Test\"
    Const FileName As String = "MyFileName"
    Const FileExt As String = ".txt"
    Dim IncrVal As Integer = 1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim stream As FileStream = New FileStream(FileDirectory & FileName & IncrVal & FileExt, FileMode.Create)
        Dim streamwriter As New StreamWriter(stream)
        
       ' code to write to file

        streamwriter.Close()
        stream.Close()

        IncrVal = IncrVal + 1

        PrgGraphBar.Value = 0

    End Sub

Open in new window

0
 
NorieConnect With a Mentor VBA ExpertCommented:
Do you mean you want to hard-code the filename and path in the code rather than using the file dialog?
Const FileDirectory As String = "C:\Test\"
Const FileName As String = "MyFileName.txt"

Dim stream As FileStream = New FileStream(FileDirectory & FileName, FileMode.Create)
Dim streamwriter As New StreamWriter(stream)

    SaveAs = FileName

    'Add in Saved file header Information first
    StreamWriter.WriteLine (Now)
    StreamWriter.WriteLine ("Collected Data from G-WHIZ Distance Curve")
    StreamWriter.WriteLine ("Rate:  " & RateVar)
    StreamWriter.WriteLine ("Run Distance:  " & DistanceVar)

    For intloop = 0 To DisData.Count - 1
        System.Threading.Thread.Sleep (100)
        StreamWriter.WriteLine (DisData(intloop))

        If Value <> 100 Then
            Application.DoEvents()
            Value = Value + 0.5
            PrgGraphBar.Value = Value
        End If
    Next

    streamwriter.Close()
    stream.Close()

    PrgGraphBar.Value = 0
           

Open in new window

0
 
cmdolcetAuthor Commented:
I want to hard code the File location but I want to have a file name with an incremental number at the end so that I can save multiple files of the same data
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
cmdolcetAuthor Commented:
Norie, correct .. I believe either of your suggestions would work right?
0
 
NorieVBA ExpertCommented:
Well the second code I posted will include a number that increments in the filename each time you run the code, the second code will save with the same  filename each time.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You might consider using something like NLog, which can create incrementally numbered files. I use it for my logging in .NET apps, and it works very nicely. You can get it using the Nuget package manager in Visual Studio.
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You might want to add an additional check to see if the filename exists, and keep incrementing the counter until you find a filename that is available.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.