Question Help With StreamWriter

jackweekes

Member
Joined
Apr 22, 2010
Messages
5
Programming Experience
Beginner
Please could you help this code works fine:
VB.NET:
    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
        Call Save()




    End Sub
    Shared Sub Save()
        

        Dim Writer As StreamWriter = New StreamWriter("Data.txt")
        Writer.WriteLine("HELLO")
        Writer.Close()


        MsgBox("HELLO")



    End Sub

But when I add to the code (bellow) it fails to write to the file.
VB.NET:
    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
        OpenFileDialog1.ShowDialog()
        PictureBox1.ImageLocation = OpenFileDialog1.FileName
        MsgBox(PictureBox1.ImageLocation)

        Call Save()




    End Sub
    Shared Sub Save()
        

        Dim Writer As StreamWriter = New StreamWriter("Data.txt")
        Writer.WriteLine("HELLO")
        Writer.Close()


        MsgBox("HELLO")



    End Sub
 
just tested your code and it works fine, still writes the text file no problem at all make sure u import reference to system.io

Imports System.IO

Public Class Form1

Sub Save()
Dim Writer As StreamWriter = New StreamWriter("C:\Documents and Settings\Owner\My Documents\Data.txt")
Writer.WriteLine("HELLO")
Writer.Close()
MsgBox("HELLO")

End Sub

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

OpenFileDialog1.ShowDialog()
PictureBox1.ImageLocation = OpenFileDialog1.FileName
MsgBox(PictureBox1.ImageLocation)
Call Save()

End Sub

End Class
 
Hi

Thank you for the response, in my coding that does not work even though I have Imports System.IO

What I did have was that the setting would be saved to a text file every time a setting gets changed, I tried putting that into a single sub but that didn't work, if I post the original would you be able to help?
 
Here it is:
VB.NET:
Imports System.IO




Public Class Form1

    Dim ab As String
    Dim DesktopLocation1 As String = "c:\Documents and settings\" & SystemInformation.UserName & "\Desktop"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle

            Me.Width = Screen.PrimaryScreen.Bounds.Width
            Me.Height = Screen.PrimaryScreen.Bounds.Height - 30

            WebBrowser1.Height = Me.Height

            Dim ary(2) As String
            Dim ab As String
            Dim I As Integer = 0
            Dim FILE_NAME As String = "data.txt"
            Dim objReader As New StreamReader(FILE_NAME)
            Do Until objReader.EndOfStream

                ary(I) = objReader.ReadLine

                I = I + 1

            Loop
            PictureBox1.ImageLocation = ary(0)
            DesktopLocation1 = ary(1)
            PictureBox1.SizeMode = ary(2)
            objReader.Close()


        Catch ex As Exception
            MsgBox("No Saved Data Found.")
        End Try

        Label1.Text = "Alignment"
    End Sub



    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
        Dim FILE_NAME As String = "data.txt"
        Dim objWriter As New StreamWriter(FILE_NAME)
        OpenFileDialog1.ShowDialog()
        PictureBox1.ImageLocation = OpenFileDialog1.FileName

        
        objWriter.WriteLine(PictureBox1.ImageLocation)
        objWriter.WriteLine(DesktopLocation1)
        objWriter.WriteLine(PictureBox1.SizeMode)
        objWriter.Close()
        MsgBox("The wallpaper has been changed")



    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        If Panel1.Visible = True Then
            Panel1.Visible = False
        Else : Panel1.Visible = True
        End If


    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(DesktopLocation1)
        WebBrowser1.Visible = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
        If PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize Then
            PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
            Label1.Text = "Center Image"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage Then
            PictureBox1.SizeMode = PictureBoxSizeMode.Normal
            Label1.Text = "Normal"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.Normal Then
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            Label1.Text = "Stretch Image"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage Then
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            Label1.Text = "Zoom"
        Else
            PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
            Label1.Text = "Auto Size"
        End If
        Dim FILE_NAME2 As String = "data.txt"
        Dim objWriter2 As New StreamWriter(FILE_NAME2)

        objWriter2.WriteLine(PictureBox1.ImageLocation)
        objWriter2.WriteLine(DesktopLocation1)
        objWriter2.WriteLine(PictureBox1.SizeMode)
        objWriter2.Close()
    End Sub

    Private Sub ButtonLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLocation.Click
        Dim FILE_NAME1 As String = "data.txt"
        Dim objWriter1 As New StreamWriter(FILE_NAME1)
        FolderBrowserDialog1.ShowDialog()
        DesktopLocation1 = FolderBrowserDialog1.SelectedPath

        objWriter1.WriteLine(PictureBox1.ImageLocation)
        objWriter1.WriteLine(DesktopLocation1)
        objWriter1.Close()
        MsgBox(DesktopLocation1)
        WebBrowser1.Navigate(DesktopLocation1)



    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Visible = False
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dialog1.Show()
    End Sub


End Class

What I would like it to have the write to file in a septate procedure, so the code doesn't have to be repeated all the time (you can just call it)

Thanks for your help :)
 
' Form 1


VB.NET:
Imports System.IO
Public Class Form1
    Dim ab As String
 Dim DesktopLocation1 As String = System.Environment.SpecialFolder.Desktop
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        Me.Width = Screen.PrimaryScreen.Bounds.Width
        Me.Height = Screen.PrimaryScreen.Bounds.Height - 30
        Me.WebBrowser1.Height = Me.Height
        Module1.load()

    End Sub
    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
        OpenFileDialog1.Filter = "jpg (*.jpg) |*.jpg;*.jpeg"
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.ImageLocation = OpenFileDialog1.FileName
            MsgBox("The wallpaper has been changed")
            Module1.save()
        End If
    End Sub
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        If Panel1.Visible = True Then
            Panel1.Visible = False
        Else : Panel1.Visible = True
        End If
    End Sub
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(DesktopLocation1)
        WebBrowser1.Visible = True
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize Then
            PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
            Label1.Text = "Center Image"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage Then
            PictureBox1.SizeMode = PictureBoxSizeMode.Normal
            Label1.Text = "Normal"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.Normal Then
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            Label1.Text = "Stretch Image"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage Then
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            Label1.Text = "Zoom"
        Else
            PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
            Label1.Text = "Auto Size"
        End If
        Module1.save()
    End Sub
    Private Sub ButtonLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLocation.Click
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            DesktopLocation1 = FolderBrowserDialog1.SelectedPath
            MsgBox(DesktopLocation1)
            WebBrowser1.Navigate(DesktopLocation1)
            Module1.save()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Visible = False
    End Sub
End Class

' Module 1
VB.NET:
Imports System.IO
Module Module1
    Dim FILE_NAform1 As String = (System.Environment.CurrentDirectory + "\data.txt")
    Sub save()
        Dim objWriter1 As New StreamWriter(FILE_NAform1)
        objWriter1.WriteLine(Form1.PictureBox1.ImageLocation)
        objWriter1.WriteLine(Form1.FolderBrowserDialog1.SelectedPath)
        objWriter1.WriteLine(Form1.PictureBox1.SizeMode)
        objWriter1.Close()
    End Sub
    Sub load()
        Try
            Dim ary(2) As String
            Dim I As Integer = 0

            Dim objReader As New StreamReader(FILE_NAform1)
            Do Until objReader.EndOfStream
                ary(I) = objReader.ReadLine
                I = I + 1
            Loop
            Form1.PictureBox1.ImageLocation = ary(0)
            Form1.FolderBrowserDialog1.SelectedPath = ary(1)
            Form1.PictureBox1.SizeMode = ary(2)
            objReader.Close()
        Catch ex As Exception
            MsgBox("No Saved Data Found.")
            Form1.WebBrowser1.Navigate(System.Environment.SpecialFolder.Desktop)
        End Try
        Form1.Label1.Text = "Alignment"
    End Sub
End Module



give this one a shot, let me know how it goes. :D
 
I managed to get the desktop folder working bellow too, its in red :)

VB.NET:
Imports System.IO
Public Class Form1
    Dim ab As String
    Dim DesktopLocation1 As String = [COLOR="red"]Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)[/COLOR]
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        Me.Width = Screen.PrimaryScreen.Bounds.Width
        Me.Height = Screen.PrimaryScreen.Bounds.Height - 30
        Me.WebBrowser1.Height = Me.Height
        Module1.load()

    End Sub
    Private Sub ButtonBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBrowse.Click
        OpenFileDialog1.Filter = "jpg (*.jpg) |*.jpg;*.jpeg"
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.ImageLocation = OpenFileDialog1.FileName
            MsgBox("The wallpaper has been changed")
            Module1.save()
        End If
    End Sub
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        If Panel1.Visible = True Then
            Panel1.Visible = False
        Else : Panel1.Visible = True
        End If
    End Sub
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(DesktopLocation1)
        WebBrowser1.Visible = True
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize Then
            PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
            Label1.Text = "Center Image"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage Then
            PictureBox1.SizeMode = PictureBoxSizeMode.Normal
            Label1.Text = "Normal"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.Normal Then
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            Label1.Text = "Stretch Image"
        ElseIf PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage Then
            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
            Label1.Text = "Zoom"
        Else
            PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
            Label1.Text = "Auto Size"
        End If
        Module1.save()
    End Sub
    Private Sub ButtonLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLocation.Click
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            DesktopLocation1 = FolderBrowserDialog1.SelectedPath
            MsgBox(DesktopLocation1)
            WebBrowser1.Navigate(DesktopLocation1)
            Module1.save()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.Visible = False
    End Sub
End Class

' Module 1

Module Module1
    Dim FILE_NAform1 As String = (System.Environment.CurrentDirectory + "\data.txt")
    Sub save()
        Dim objWriter1 As New StreamWriter(FILE_NAform1)
        objWriter1.WriteLine(Form1.PictureBox1.ImageLocation)
        objWriter1.WriteLine(Form1.FolderBrowserDialog1.SelectedPath)
        objWriter1.WriteLine(Form1.PictureBox1.SizeMode)
        objWriter1.Close()
    End Sub
    Sub load()
        Try
            Dim ary(2) As String
            Dim I As Integer = 0

            Dim objReader As New StreamReader(FILE_NAform1)
            Do Until objReader.EndOfStream
                ary(I) = objReader.ReadLine
                I = I + 1
            Loop
            Form1.PictureBox1.ImageLocation = ary(0)
            Form1.FolderBrowserDialog1.SelectedPath = ary(1)
            Form1.PictureBox1.SizeMode = ary(2)
            objReader.Close()
        Catch ex As Exception
            MsgBox("No Saved Data Found.")
            Form1.WebBrowser1.Navigate[COLOR="Red"](Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop))[/COLOR]
        End Try
        Form1.Label1.Text = "Alignment"
    End Sub
End Module
 
Back
Top