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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
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
NorieAnalyst Assistant Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

cmdolcetAuthor Commented:
Norie, correct .. I believe either of your suggestions would work right?
0
NorieAnalyst Assistant Commented:
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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.