Here's what i've got so far, i've written all of the data that i'm going to put into my config file, however i'm having trouble reading the specific string values that I need from that ini file. The parts of the ini lines I want to get are the values in between the ' ' in value='____'.
I've tried using streamreader a couple things but i'm having trouble figuring that out. I need to parse each line of the ini file to get that value in between the single quotations, so that I can set the value for my richtextbox background and text color as well as a few other things that i've set up in my notepad including tab spacing and so on. Help would be much appreciated because lots of these different values need to be converted from a string value to integers, and boolean values as well. (4 of them, the rest of them don't have to be changed because they are already string values when my application reads them to determine it's application load settings.
EDIT: I just noticed in the preview that this is from the version of the app where I had streamwriter write "______ value=" & Something to a file. lol some of the (')s are still in this version, so please excuse that little part. I had them in, but took them out so that I could trim data to get a value easier, but I want to use (')s hopefully when all is said and done.
For example, just to restate what I want - in the ini file it will read
[setting name] value='VALUE'
I want to be able to "get" the string value of "VALUE" within the ini file between the 2 single quotations ' & '.
Thanks
Last edited by AceInfinity; May 23rd, 2011 at 12:56 AM.
note that "'" is a single cuotation between two doubles.
InStr will find the location of the first appearance of ' on the old string.
Mid will return the middle part of the old string, begining on the second parameter, with length of the third
On the third paramenter i am taking the length of the old string - the position of the first ' to eliminate all that part, -1 to take care of the last '
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
wow, that actually should work in my case. Thanks for putting it in a way that I could understand too, most people just give me the code lol, but I like to learn as well. +1 for you
I just need to figure out how to get it to read a specific line that I want it to read so that I know what value i'm getting from the line i'm 'trimming' using that code. That's one part that I don't understand, If I did I would have used a similar trimming method like you explained though.
I want to make sure that the lines i'm trimming, I know what they are so that I can use those values to define the configuration for the right settings.
If you want to know the field name, like "Editor Text Color" or "Font Type" then use similar code I showed you but look for the "[" and "]" using the InStr function.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
I tried debugging by returning the value of an example value using a msgbox but it wouldn't return anything after pressing the loaddefault button, so i'm assuming it's not reading any of those values, meaning my line of code for SettingName isn't quite right
Last edited by AceInfinity; May 23rd, 2011 at 04:27 PM.
Off the top I see that you do not loop through all the lines but only
Dim OriginalString As String = ConfigFile.ReadLine
For structure purposes try using CASE SELECT instead of all those IF statements
If you want to debug its better to put a Break line (F9) and then, once stopped, step through the code with F8 using the watch or local windows to see the values of variables.
Dim line As String = "[Variable Name] value='test'"
Get the variable name:
Code:
Dim varname As String = line.Remove(0, line.IndexOf("[") + 1)
varname = varname.Substring(0, varname.IndexOf("]"))
Get the value (only check using the ' signs):
Code:
Dim value1 As String = line.Remove(0, line.IndexOf("'") + 1)
value1 = value1.Substring(0, value1.IndexOf("'"))
Get the value (match value= as well):
Code:
Dim value2 As String = line.Remove(0, line.IndexOf("value=") + 6).Trim("'").Trim()
You can try to parse everything using a single line, but the risk exists you forget to subtract the start index of the length to get the substring (mid) value.
Example I quickly wrote:
vb Code:
Public Class Configuration
Sub New(ByVal SettingsFile As String)
Me.Load(SettingsFile)
End Sub
Public Sub Load(ByVal SettingsFile As String)
If System.IO.File.Exists(SettingsFile) Then
Try
Dim reader As New System.IO.StreamReader(SettingsFile)
Do While reader.Peek <> -1
Dim textline As String = reader.ReadLine
'verify the line is valid
If textline.Contains("[") AndAlso textline.Contains("'") Then
Dim settingname As String = textline.Remove(0, textline.IndexOf("[") + 1)
Dim value As String = textline.Remove(0, textline.IndexOf("value=") + 6).Trim("'").Trim()
'set the correct variable based on the name
Select Case settingname.ToLower
Case "editor text color"
Me.CTColor = value
Case "editor background color"
Me.CBColor = value
Case "application title color"
Me.CFormTitleColor = value
Case "top menu color"
Me.CTMenuColor = value
Case "bottom status menu color"
Me.CBMenuColor = value
Case "font type"
Me.CFontType = value
Case "font size"
Me.CFontSize = value
Case "tab spacing size"
Me.CTabSpacer = value
Case "drag drop files"
Me.CDragDrop = value
Case "always on top"
Me.COnTop = value
Case "word wrap"
Me.CWordWrap = value
Case Else
Debug.WriteLine("Setting not found: " & settingname & " with value " & value)
End Select
End If
Loop
reader.Close()
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End If
End Sub
Public CBColor As String
Public CTColor As String
Public CFormTitleColor As String
Public CTMenuColor As String
Public CBMenuColor As String
Public CFontType As String
Public CFontSize As String
Public CTabSpacer As String
Public CDragDrop As String
Public COnTop As String
Public CWordWrap As String
End Class
Code:
Dim c As New Configuration("Config.ini")
I really recommend you use a class to store you configuration information. It makes your code look a lot better, especially if you have to reload your settings multiple times during runtime.
Alternatively you could add a single hashtable and write key/value pairs to it. This way it would work with any variable:
Code:
Table = New Hashtable()
Dim reader As New System.IO.StreamReader(SettingsFile)
Do While reader.Peek <> -1
Dim textline As String = reader.ReadLine
'verify the line is valid
If textline.Contains("[") AndAlso textline.Contains("'") Then
Dim settingname As String = textline.Remove(0, textline.IndexOf("[") + 1)
settingname = settingname.Substring(0, settingname.IndexOf("]"))
Dim value As String = textline.Remove(0, textline.IndexOf("value=") + 6).Trim("'").Trim()
Table.Add(settingname, value)
End If
Loop
reader.Close()
Code:
Dim CTextColor As String = Table.Item("Editor Text Color")
EDIT
Kaliman your code has one issue: If the reader reaches the end another line is read; thus at least one "Nothing" line is read (by user code, not the reader) which could cause null reference exceptions.
You'd have to add another If block around it checking if the newly read line is nothing. It makes code longer, thus I like to stick with the Peek method.
Code:
Do
OriginalString = ConfigFile.ReadLine()
If OriginalString Is Nothing Then
Exit Do
Else
NewString = Mid(OriginalString, InStr(OriginalString, "'"), Len(OriginalString) - InStr(OriginalString, "'") - 1)
Select Case NewString
Case "Editor Text Color"
CTColor = NewString
RichTextBox1.ForeColor = ColorTranslator.FromHtml(CTColor)
Case "Editor Background Color"
CBColor = NewString
RichTextBox1.BackColor = ColorTranslator.FromHtml(CBColor)
End Select
End If
Loop
Last edited by bergerkiller; May 23rd, 2011 at 05:57 PM.
Dim sLineOfText As String = ""
Dim bValueFound As Boolean = False
Dim iSingleQuoteIndex As Integer = 0
Dim sValue As String = ""
Dim sFileName As String = ""
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
sFileName = OpenFileDialog1.FileName()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(sFileName)
Do While sr.Peek >= 0
sLineOfText = sr.ReadLine
bValueFound = False 'set flag to false
For x As Integer = 0 To sLineOfText.Length
If CBool(sLineOfText.IndexOf("'")) Then '
iSingleQuoteIndex = sLineOfText.IndexOf("'") '
sValue = sLineOfText.Substring(iSingleQuoteIndex + 1) '
For y As Integer = 0 To sValue.Length '
If CBool(sValue.IndexOf("'")) Then '
sValue = sLineOfText.Substring(iSingleQuoteIndex + 1, sValue.IndexOf("'")) '
'
bValueFound = True
Debug.Print(sValue)
Exit For
End If
Next
End If
If bValueFound Then Exit For 'reset flag for jumping out this For/Next and back into the Do loop, if it is still going
Next
Loop
sr.Close()
End If
or 2 If Statements:
Code:
Dim sLineOfText As String = ""
Dim iSingleQuoteIndex1 As Integer = 0
Dim iSingleQuoteIndex2 As Integer = 0
Dim sValue As String = ""
Dim sFileName As String = ""
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
sFileName = OpenFileDialog1.FileName()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(sFileName)
Do While sr.Peek >= 0
sLineOfText = sr.ReadLine
If sLineOfText.Contains("'") Then '
iSingleQuoteIndex1 = sLineOfText.IndexOf("'") '
sValue = sLineOfText.Substring(iSingleQuoteIndex1 + 1) '
If sValue.Contains("'") Then
iSingleQuoteIndex2 = sValue.IndexOf("'")
sValue = sLineOfText.Substring(iSingleQuoteIndex1 + 1, iSingleQuoteIndex2) '
Debug.Print(sValue)
End If
End If
Loop
sr.Close()
End If
bergerkiller, your information really helped after trying several different methods, I think I finally understand what I was doing wrong from the start. I had the value's and the setting name coming out fine. I tested using a quick msgbox to get the values of both, but your code narrows mine down a bit more I think.
Code:
Case "tab spacing size"
Me.CTabSpacer = value
That I actually have set to FormTabs instead of me because it's not on the same form.
And one more thing, could I not use:
Code:
Do While ConfigFile.ReadLine.StartsWith("[")
Last edited by AceInfinity; May 23rd, 2011 at 08:40 PM.
The code I have now still doesn't seem to work. It only loops through the first setting for some reason, at least it appears that way... I just want it in a sub for now so I can understand it first.
vb Code:
Private Sub LoadDefaultConfig()
Dim ConfigFile As New StreamReader("Config.ini")
Dim OriginalString As String = ConfigFile.ReadLine()
Dim CBColor As String
Dim CTColor As String
Dim CFormTitleColor As String
Dim CTMenuColor As String
Dim CBMenuColor As String
Dim CFontType As String
Dim CFontSize As String
Dim CTabSpacer As Integer
Dim CDragDrop As Boolean
Dim COnTop As Boolean
Dim CWordWrap As Boolean
Try
Do While ConfigFile.ReadLine.StartsWith("[")
If OriginalString.Contains("[") AndAlso OriginalString.Contains("'") Then
'Get the value of the Setting Name
Dim SettingName As String = OriginalString.Remove(0, OriginalString.IndexOf("[") + 1)
Seems like you are doing much more than necessary when there is My.Settings to store, retrieve and save options or use an XML configuration file to store, retrieve and save options.
I agree with you on that KEvin, when it involves many settings like this, and I am sure it will grow -lol, xml is nice, clean and fast.
My.settings was like a godsend when I found that when .Net came out.
-
It's fun too, though, to search through the strings like Ace is doing.
I like it all.
I'm too easy
Last edited by proneal; May 23rd, 2011 at 09:59 PM.
Reason: i Misspelled -:P
I started using an xml app.config file, but I want to be able to use a .ini file. I don't want to use the app settings either, I want a settings file seperate from the application as a .ini file extension to get the settings as an older method of configuration.
And yes, I did remove the ToLower so that it didn't read only lower case strings, but I didn't know that got posted with the first code of mine I posted lol, my fault. proneal you fixed the code finally though lol, After doing that it finally went through the lines of my .ini file, thanks
I spent way too long on this part of my application, through trying different methods and troubleshooting. For what I want the settings to do though, a .ini file seems more appropriate since xml isn't designed to be written to from what i've heard. xml was also invented so it could handle more advanced data for an application's configuration, but for what i'm using it for I don't see a need to use it. I could have used application settings, but I want a more "visual" configuration instead. You can view all of the settings directly from the .ini file in this case, and I can create more setting's files If I wanted to, or add them all onto the same one i'm creating with a little bit of work.
Last edited by AceInfinity; May 23rd, 2011 at 10:30 PM.
It's your program Ace.
YOU call the shots, lol
Fun to code, aint it..
No matter which way/method -when it works, it makes life that much grander.
I'm pathetic, i know
-
Glad you got it going Ace.
I love coding though, the feeling after finally figuring something out is good. I just don't like it when I'm stuck after a while of trying different methods and troubleshooting.
lol for some reason though I need to figure out why my loop is checking every second line. I thought it wasn't validating some lines, until I checked the ini file itself, and I figured out that the pattern it was making was skipping every second line. So I still have a minor problem, but i'm that much closer....
My code so far:
Last edited by AceInfinity; May 23rd, 2011 at 11:23 PM.
'Me.RichTextBox1.Font = New System.Drawing.Font(RichTextBox1.Text, CFontType)
Case "Font Size"
CFontSize = Value
'Me.RichTextBox1.Font = New System.Drawing.Font(RichTextBox1.Text, CFontSize)
Case "Tab Spacing Size"
CTabSpacer = CInt(Value)
'FormTabs.NumericUpDown1.Value = CTabSpacer
Case "Drag Drop Files"
CDragDrop = CBool(Value)
'Me.RichTextBox1.AllowDrop = CDragDrop
Case "Always On Top"
COnTop = CBool(Value)
'Me.TopMost = COnTop
Case "Word Wrap"
CWordWrap = CBool(Value)
'Me.RichTextBox1.WordWrap = CWordWrap
Case Else
Debug.WriteLine("Setting not found: " & SettingName & " with the value '" & Value & "'")
End Select
End If
Loop
Catch ex As Exception
MsgBox(ex.Message)
End Try
ConfigFile.Close()
End Sub
I never understand the mycode for the vbcode, whenever I use [ HIGHLIGHT ][ /HIGHLIGHT ] as the surrounding brackets for my code it just turns my code into a bunch of red text, yet when I use the button for VBCode, and put my code between those exact same 2 mycodes, it works...
Edit: now that i'm looking at this code, I think the problem is that with the trim, it's moving the second line of text beside the line is just read by removing a trailing space, and so when it moves to the next line, the line after the one it's suppose to read gets moved up because the second line gets bumped to the same line as the setting it just read. I'll edit and test it out.
Update: Nope, my theory was incorrect, I tried not using the trim function and tried
Code:
Dim Something As String = OriginalString.Remove(0, OriginalString.IndexOf("value=") + 6)
Dim Value As String = Something.Replace("'", "")
But nothing changed, still reads every second line in the .ini.... If I ever get this figured out i'll provide a download link for my program if it doesn't go against any rules here for everyone lol.
hmm I should also fix my font size falue and convert it to Single, so that that .25 doesn't show up. never noticed it before, I just figured that I got the saving the .ini file part down that I'd move onto the read part.
Last edited by AceInfinity; May 23rd, 2011 at 11:38 PM.
Case "Font Size"
CFontSize = CSng(Value)
'Me.RichTextBox1.Font = New System.Drawing.Font(RichTextBox1.Text, CFontSize)
Me.RichTextBox1.Font = New System.Drawing.Font(RichTextBox1.Text, CFontSize)
Case "Font Type"
CFontType = Value
'Me.RichTextBox1.Font = New System.Drawing.Font(RichTextBox1.Text, CFontType)
Me.RichTextBox1.Font = New System.Drawing.Font(RichTextBox1.Text, FontName.CfontType)
The code needed to define a new font type is Richtextbox.font = New System.Drawing.Font(RichTextBox1.Text, FONTSIZE, FONTTYPE)
And since I dont have a font size as a value, how can I put in a font type without having that value within that case? When I debug it, it keeps saying that it cannot convert "courier new" to single, so I get the value of the font type, but it's trying to parse it as data for the font size.
I couldn't put 2 values into the .ini file on the same line to get them both at once or it would mess up all of the trimming to get my settings variable and my values.
Last edited by AceInfinity; May 24th, 2011 at 12:58 AM.
Haven't had time to open the IDE and try things out for you.
It might be alright to initialize all these variables with DEFAULT values.
This is good because you wouldn't have to carry the ini with your package setup.
The program could write the ini file on it's first start up. with all the default values.
Then it can just replace the values as needed with the Options/Settings form- at some later time when User decides to change things up.
-
All the best to you Ace!
Ahh, lol you and me think alike Thats the way I just finished coding it out. I just let it run LoadDefaultConfig() on form load, and if the file used for loading the default config doesn't exist (Config.ini)... Then it will load the program with the applications default settings, and create that ini file with the default app settings for the file. I also have a button that will open the file directly into the editor for manual modification if someone wants to do it that way, but now the app should do everything for you.
Here's a download if any of you would like to give it a test Hopefully i'm allowed to provide a sample of what i'm working on like this because I haven't been able to find the rules on this forum very easily.
Thanks for all of the help. I'm still continuing my development on this project. When you run it, it will create a config file in the same directory as the app, so I'll create a setup.exe to place it in the program folders location on install like I did with version 3.0. You can create a folder manually though in program folder, and then create a shortcut on the desktop so that you don't have to deal with the config file taking up space on your desktop with the application.
I figured out the font variables by parsing the data to labels that i've kept hidden on the actual form, so once the loop has gone through all the lines, and has discovered the value for the font size and type, it will use both of those in the font code as a new system.drawing.font. Maybe not the best way to do it, but it works, and nothing is visible to the user.
Next thing i'm adding will be a "Sort" function for lines within the editor, for alphabetical sorting.
Last edited by AceInfinity; May 24th, 2011 at 05:26 AM.
I started using an xml app.config file, but I want to be able to use a .ini file. I don't want to use the app settings either, I want a settings file seperate from the application as a .ini file extension to get the settings as an older method of configuration.
And yes, I did remove the ToLower so that it didn't read only lower case strings, but I didn't know that got posted with the first code of mine I posted lol, my fault. proneal you fixed the code finally though lol, After doing that it finally went through the lines of my .ini file, thanks
I spent way too long on this part of my application, through trying different methods and troubleshooting. For what I want the settings to do though, a .ini file seems more appropriate since xml isn't designed to be written to from what i've heard. xml was also invented so it could handle more advanced data for an application's configuration, but for what i'm using it for I don't see a need to use it. I could have used application settings, but I want a more "visual" configuration instead. You can view all of the settings directly from the .ini file in this case, and I can create more setting's files If I wanted to, or add them all onto the same one i'm creating with a little bit of work.
First off good to hear you got things working but wanted to address what you said about xml isn't designed to be written to. Check out the attached VS2008 project which demos how easy it is to read/write to and from a XML settings file. In the demo the file is stored in the app folder but could easily be stored any place you wanted. Also note in the form load event there is a good deal of assertion in that if there are no values set on first time usage it will provide default values. Lastly for an actual app all the code shown would be housed in a class that you would create once and use all over the app. Time to create this demo was roughly 30 minutes.
Example from the demo for read/write font information
Code:
Dim FontInformation = _
( _
From B In Document...<Font> _
Select Family = B.<Type>.Value, _
Size = If(String.IsNullOrEmpty(B.<Size>.Value), 8, _
CSng(B.<Size>.Value))).FirstOrDefault
If FontInformation IsNot Nothing Then
Dim MyFont = New Font(FontInformation.Family, _
FontInformation.Size, _
Me.Font.Style)
FontDialog1.Font = MyFont
End If
If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Query = (From B In Document...<EditorSettings> Select B.<Font>).FirstOrDefault
Query.<Type>.Value = FontDialog1.Font.FontFamily.Name
Query.<Size>.Value = FontDialog1.Font.Size.ToString
Document.Save(SettingsFile)
End If
Re: [RESOLVED] Reading Data From Lines of .ini File
Yeah I know it's possible, i'm a programmer, but I don't specifically code in VB.net. XML can be written to, but I just said it wasn't really designed for that, specifically for app settings for an application. Since XML can hold more advanced and complex data, if you have some of that information stored into an XML file, it shouldn't be that easily changed. So the more common way of initializing a settings file is through an .ini file, and by keeping an XML file dedicated to the more advanced data for the application config.
Re: [RESOLVED] Reading Data From Lines of .ini File
Originally Posted by AceInfinity
Yeah I know it's possible, i'm a programmer, but I don't specifically code in VB.net. XML can be written to, but I just said it wasn't really designed for that, specifically for app settings for an application. Since XML can hold more advanced and complex data, if you have some of that information stored into an XML file, it shouldn't be that easily changed. So the more common way of initializing a settings file is through an .ini file, and by keeping an XML file dedicated to the more advanced data for the application config.
ini files are not the recommended method to store settings anymore, that is old news. Examples, your project and solution files all use XML to store settings, Microsoft Office 2007 stores some settings in XML while other settings in the system registry. Lastly Adobe Fireworks stores settings in XML. Not to beat an old dog to death but XML is the way to go, no API's to read/write to them or code you have to bend over backwards to understand.
Any ways it is your choice and will leave it at that, just wanted to give you a good current method to read/write your settings.
Re: [RESOLVED] Reading Data From Lines of .ini File
That's exactly what I meant though, I understand the difference between both, but for someone to edit an xml file to change settings like boolean values for true or false on things like word wrap, always on top, detect urls, etc... Including color values and an integer value for the tab spacing on my app would be chaotic, and the greater the chances of someone screwing up. I wanted a config file that was easy to edit manually and xml was the worst way to go in this case. It's designed to hold more complex configuration data. Originally .ini files did that, then Microsoft came out with the registry, and xml was invented, xml is too complex in this case.
Your example of Adobe fireworks... Do you really think that a notepad application like this is as advanced as that? lol. People also don't go ahead and edit that xml file directly. This .ini file i've associated with my app was far better suited for the job. If I need a config file for more complex information i'll use xml, but chances are people won't be editing that xml file for their user settings, instead of the ini file that i've created because it's simplistic.
Re: [RESOLVED] Reading Data From Lines of .ini File
Originally Posted by AceInfinity
I wanted a config file that was easy to edit manually
This was never specified in your original question.
INI or XML configuration files are not supposed to be edited manually but instead by the application or program that uses them.
Originally Posted by AceInfinity
Including color values and an integer value for the tab spacing on my app would be chaotic, and the greater the chances of someone screwing up.
This is why people should not be editing them. If the developer wants to edit them then fine, if they screw it up then there is something really wrong if this is the developer who wrote the code. Better to have a options dialog to change information no matter if the config file is INI or XML.
In regards to
Originally Posted by AceInfinity
Do you really think that a notepad application like this is as advanced as that?
That is not the issue, instead a developer should be consistent in how they code which in this case is reading settings from a configuration file.
Re: [RESOLVED] Reading Data From Lines of .ini File
This was never specified in your original question.
INI or XML configuration files are not supposed to be edited manually but instead by the application or program that uses them.
It should make sense to you though that having a user manually edit a config file would make it easier for them to edit the .ini i'm giving them instead of a whole bunch of xml having parts that don't require any editing. Only the bottom half that includes the application settings.
.ini file's are created to be written to manually or programmatically. Please don't come on my thread and tell me any different because i'm more familiar with ini than I am in xml.
This is why people should not be editing them. If the developer wants to edit them then fine, if they screw it up then there is something really wrong if this is the developer who wrote the code. Better to have a options dialog to change information no matter if the config file is INI or XML.
my comment there was about xml, not ini at all. ini file's aren't complicated if they are meant to be config files for specific things, whereas you can't really control all of the extra stuff that isn't to be changed in an xml file.
If they screw it up on my app, it will revert to the default application setting automatically, so that's already handled. If I had that for xml, that wouldn't be the case, things would screw up.
That is not the issue, instead a developer should be consistent in how they code which in this case is reading settings from a configuration file.
It is the issue, i'm not asking the ini file to carry out anything complicated, so thats why I would never need to have used xml instead. xml was created because ini couldn't deal with complex tasks. so the registry was also invented at the time by microsoft.
what are you saying about consistency here? consistent in what? I am consistent in my code but between xml and ini just because you use xml doesn't mean you have to always use xml, and vice versa. Consistency is important in programming, but not in this case if i'm understanding this correctly.
Using xml for advanced applications and for even a basic button press application is completely unorthodox. You don't have to make things more complicated on the program side of the spectrum if it's not needed. If I was to give someone access to edit a button press program through xml and compare to ini, that's a difference of quite a few lines of code that they would have to look through to find the actual configuration part of it. xml is not necessary here.