Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reading a text file and saving to settings.


  • Please log in to reply
7 replies to this topic

#1 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts
  • Location:West Sussex, England
  • Programming Language:Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Transact-SQL

Posted 27 August 2010 - 02:26 AM

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!
  • 0

#2 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 27 August 2010 - 02:47 AM

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?
  • 0

#3 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts
  • Location:West Sussex, England
  • Programming Language:Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Transact-SQL

Posted 27 August 2010 - 05:45 AM

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?
  • 0

#4 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 27 August 2010 - 09:31 AM

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?
  • 0

#5 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts
  • Location:West Sussex, England
  • Programming Language:Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Transact-SQL

Posted 29 August 2010 - 12:27 AM

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

#6 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 30 August 2010 - 02:12 PM

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 = ""

  • 0

#7 blindtrevor

blindtrevor

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 17 September 2010 - 05:39 AM

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

  • 0

#8 blindtrevor

blindtrevor

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 17 September 2010 - 05:42 AM

Hmmm... actually - this will just save each line to Setting1... Hmmmm?

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


  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download