-
Feb 26th, 2012, 09:21 AM
#1
Thread Starter
Addicted Member
[RESOLVED] My.settings Load From external file
I have a project need to reset the my.settings by external files, i have problem to get back the system.drawing.color ,please help:-
Frist I use the SaveConfigFromMySettingToFile() to get the config file,then i add the LoadConfigFromFileToMySetting() at the FormLoad action,But Nothing change! Why? without the LoadConfigFromFileToMySetting() at system load i got the correct color of the button from my.settings.
Code:
Imports System.IO
Public Class Form1
Public ArrayListLoadConfigFromFile As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadConfigFromFileToMySetting()
Button1.BackColor = My.Settings.color0
Button2.ForeColor = My.Settings.color1
Button3.BackColor = My.Settings.color2
Button4.ForeColor = My.Settings.color3
Button5.BackColor = My.Settings.color4
End Sub
Public Sub LoadConfigFromFileToMySetting()
Dim stream_reader As New IO.StreamReader(My.Application.Info.DirectoryPath & "/data/config.dat", System.Text.Encoding.UTF8)
Dim line As String
Try
' Read the file one line at a time.
line = stream_reader.ReadLine()
Do While Not (line Is Nothing)
' Trim and make sure the line isn't blank.
line = line.Trim()
If line.Length > 0 Then ArrayListLoadConfigFromFile.Add(line)
' Get the next line.
line = stream_reader.ReadLine()
Loop
stream_reader.Close()
Catch exc As Exception
' Report all errors.
MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & "Error")
End Try
My.Settings.color0 = ArrayListLoadConfigFromFile.Item(0)
My.Settings.color1 = ArrayListLoadConfigFromFile.Item(1)
My.Settings.color2 = ArrayListLoadConfigFromFile.Item(2)
My.Settings.color3 = ArrayListLoadConfigFromFile.Item(3)
My.Settings.color4 = ArrayListLoadConfigFromFile.Item(4)
End Sub
Public Sub SaveConfigFromMySettingToFile()
ArrayListLoadConfigFromFile.Clear()
ArrayListLoadConfigFromFile.Add(My.Settings.color0)
ArrayListLoadConfigFromFile.Add(My.Settings.color1)
ArrayListLoadConfigFromFile.Add(My.Settings.color2)
ArrayListLoadConfigFromFile.Add(My.Settings.color3)
ArrayListLoadConfigFromFile.Add(My.Settings.color4)
If File.Exists(My.Application.Info.DirectoryPath & "/data/config.dat") = True Then
Kill(My.Application.Info.DirectoryPath & "/data/config.dat")
End If
Dim i As Integer
Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "/data/config.dat", True, System.Text.Encoding.UTF8)
For i = 0 To ArrayListLoadConfigFromFile.Count - 1
SW.WriteLine(ArrayListLoadConfigFromFile(i))
Next
End Using
End Sub
End Class
/data/config.dat shown as beow ,which created by SaveConfigFromMySettingToFile() before LoadConfigFromFileToMySetting() is activated
Code:
Color [Black]
Color [White]
Color [Silver]
Color [Blue]
Color [Silver]
Last edited by edwinho; Feb 26th, 2012 at 09:52 PM.
Reason: [RESOLVED]
-
Feb 26th, 2012, 02:44 PM
#2
Re: My.settings Load From external file
Since your items are Color, not String you need to convert from String to Color as shown in this hardwired example.
Code:
Dim MyColorAsString As String = "Black"
Dim Converter As New ColorConverter()
My.Settings.Color1 = DirectCast(Converter.ConvertFromString(MyColorAsString), Color)
-
Feb 26th, 2012, 03:08 PM
#3
Re: My.settings Load From external file
Or just save the colors as strings and convert them back, not pretty but it works.
Code:
Public Class Form1
Private configPath As String = "R:\TEMP\config.dat"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
LoadConfigFromFileToMySetting()
Button1.BackColor = My.Settings.color0
Button2.ForeColor = My.Settings.color1
Button3.BackColor = My.Settings.color2
End Sub
Private Sub LoadConfigFromFileToMySetting()
If Not IO.File.Exists(configPath) Then
Return
Else
Dim newColors As New List(Of String)
newColors.AddRange(IO.File.ReadAllLines(configPath))
My.Settings.color0 = Color.FromArgb(CInt(newColors(0)))
My.Settings.color1 = Color.FromArgb(CInt(newColors(1)))
My.Settings.color2 = Color.FromArgb(CInt(newColors(2)))
End If
End Sub
Private Sub SaveConfigFromMySettingToFile()
Dim myColors As New List(Of String)
myColors.AddRange({My.Settings.color0.ToArgb.ToString, _
My.Settings.color1.ToArgb.ToString, _
My.Settings.color2.ToArgb.ToString})
IO.File.WriteAllLines(configPath, myColors.ToArray())
End Sub
End Class
-
Feb 26th, 2012, 09:56 PM
#4
Thread Starter
Addicted Member
Re: [RESOLVED] My.settings Load From external file
Thank !! It work for me !!
Code:
Imports System.IO
Public Class Form1
Public ArrayListLoadConfigFromFile As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadConfigFromFileToMySetting()
Button1.BackColor = My.Settings.color0
Button2.ForeColor = My.Settings.color1
Button3.BackColor = My.Settings.color2
Button4.ForeColor = My.Settings.color3
Button5.BackColor = My.Settings.color4
End Sub
Public Sub LoadConfigFromFileToMySetting()
Dim stream_reader As New IO.StreamReader(My.Application.Info.DirectoryPath & "/data/config.dat", System.Text.Encoding.UTF8)
Dim line As String
Try
' Read the file one line at a time.
line = stream_reader.ReadLine()
Do While Not (line Is Nothing)
' Trim and make sure the line isn't blank.
line = line.Trim()
If line.Length > 0 Then ArrayListLoadConfigFromFile.Add(line)
' Get the next line.
line = stream_reader.ReadLine()
Loop
stream_reader.Close()
Catch exc As Exception
' Report all errors.
MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & "Error")
End Try
My.Settings.Color0 = Color.FromArgb(CInt(ArrayListLoadConfigFromFile.Item(0)))
My.Settings.Color1 = Color.FromArgb(CInt(ArrayListLoadConfigFromFile.Item(1)))
My.Settings.Color2 = Color.FromArgb(CInt(ArrayListLoadConfigFromFile.Item(2)))
My.Settings.Color3 = Color.FromArgb(CInt(ArrayListLoadConfigFromFile.Item(3)))
My.Settings.Color4 = Color.FromArgb(CInt(ArrayListLoadConfigFromFile.Item(4)))
End Sub
Public Sub SaveConfigFromMySettingToFile()
ArrayListLoadConfigFromFile.Clear()
ArrayListLoadConfigFromFile.Add(My.Settings.Color0.ToArgb.ToString)
ArrayListLoadConfigFromFile.Add(My.Settings.Color1.ToArgb.ToString)
ArrayListLoadConfigFromFile.Add(My.Settings.Color2.ToArgb.ToString)
ArrayListLoadConfigFromFile.Add(My.Settings.Color3.ToArgb.ToString)
ArrayListLoadConfigFromFile.Add(My.Settings.Color4.ToArgb.ToString)
If File.Exists(My.Application.Info.DirectoryPath & "/data/config.dat") = True Then
Kill(My.Application.Info.DirectoryPath & "/data/config.dat")
End If
Dim i As Integer
Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "/data/config.dat", True, System.Text.Encoding.UTF8)
For i = 0 To ArrayListLoadConfigFromFile.Count - 1
SW.WriteLine(ArrayListLoadConfigFromFile(i))
Next
End Using
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|