Jump to content

Reading a text file and saving to settings.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Okay so I wont tell you exactly what im doing as it's kind of a secret..

My program is going to open up a text file into a textbox..I then want the program to change my.settings according to the colour. For example, setting1 is line1 of the textbox, setting2 is line2 of the textbox.
Here's my text file

brown

green

yellow

orange

so what I want it to do is assign a line to a setting. Line 1 for setting1, line 2 for setting2. And so on, I need it so it saves the line as the setting. So if line 1 = brown then setting1 = brown.
Could somebody please help me out a bit here?

Thanks a bunch!

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You can either split the string at each line and add each line to the correct setting or you can use IO.File.ReadAllLines to get a string already. Or is the problem that you doesn't know from the beginning how many settings you need?

#3
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
What I'm looking for is a code that will pick out each line. I want it to assign a line to a setting. Obviously line 1 will be setting1 and line 2 is setting 2. I just dont know how to do this?

#4
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
But what part doesn't you know? Do you know how to get it line by line? Or do you know how to assign the values to settings?

#5
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
I don't know both. I haven't really worked with streamreader before. So I'm a bit stuck :\

#6
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I guess something like this could work, I haven't tried it though.

Open File and Saving the settings:
        Dim content() As String = IO.File.ReadAllLines("C:\example\example.txt")

        For i As Integer = 0 To content.GetUpperBound(0)

            SaveSetting("myAppName", "Settings", "Setting" & i + 1, content(i))

        Next

Loading the settings (and here showing them in a messagebox each):
        Dim settingCount As Integer = 1

        Dim content As String

        Do

            content = GetSetting("myAppName", "Settings", "Setting" & settingCount)

            If content <> "" Then

                MsgBox(content)

                settingCount += 1

            End If


        Loop Until content = ""


#7
blindtrevor

blindtrevor

    Newbie

  • Members
  • Pip
  • 6 posts
How about domething like this...?


Imports System.IO


Public Class Form1



Dim FileReader As StreamReader

Dim fileLocation As String = "c:\text.txt"



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     If My.Computer.FileSystem.FileExists(fileLocation) Then

            FileReader = New StreamReader(fileLocation)

            Dim strLine As String

            Do

                strLine = FileReader.ReadLine

                If strLine <> Nothing Then

                    My.Settings.Setting1 = strLine

                    My.Settings.Save()

                End If

            Loop While strLine <> Nothing

            FileReader.Close()

        End If

End Sub

End Class


#8
blindtrevor

blindtrevor

    Newbie

  • Members
  • Pip
  • 6 posts
Hmmm... actually - this will just save each line to Setting1... Hmmmm?

blindtrevor said:

How about domething like this...?


Imports System.IO


Public Class Form1



Dim FileReader As StreamReader

Dim fileLocation As String = "c:\text.txt"



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     If My.Computer.FileSystem.FileExists(fileLocation) Then

            FileReader = New StreamReader(fileLocation)

            Dim strLine As String

            Do

                strLine = FileReader.ReadLine

                If strLine <> Nothing Then

                    My.Settings.Setting1 = strLine

                    My.Settings.Save()

                End If

            Loop While strLine <> Nothing

            FileReader.Close()

        End If

End Sub

End Class