Sequential for.ini-files?

Simon

Active member
Joined
Mar 3, 2003
Messages
25
Location
V
Hello,

For an application that Im working on I need to save the settings in .ini-files. I chose the random access to read and write to these files because it was the easiest.
Now, these .ini-files do not look good if you open them in a text editor (no carriage return) so I was thinking of changing to sequential access.

So, do any of you use settings-files within your application?
If so, do you use random or sequential acces or something else perhaps?

Thanks,
//Simon.
 
For .NET, you can use XML files, rather than INI files. If you create
a class which contains fields (private variables) for all of the settings you
wish to save.

You can then use the Xml.XmlSerializer to save your class into XML
form on disk, and also to read an XML file from disk and store the
information into the class.

Read your MSDN (serach for XmlSerializer) for more information.
 
I have found the registry is easiest to store settings. Although it is easiest I would say that XML is best.
 
Ooooo blimey ... lol :)

Okay, Im gonna try hard to understand this XML Serializer, now that I have a whole bunch of text in very difficult english to read though.

But I can manage!
Im stunned by the possibilities of .NET and how complex programming can be, now, this is gonna be a challenge for me.
Once Ive understood Ill be very happy and that is, in my opinion, the most wonderful part of programming - to finally understand.

Thanks,
SiMoN.
 
Hi folks

What is the advatage of using XML instead of .ini?
It just seems more complicated:eek:

Im sorry, the msdn guides dont help at all:(

bye,
SiMoN.
 
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sr As StreamReader = New StreamReader(New FileStream("C:\test.ini", FileMode.Open))
        TextBox1.AppendText(sr.ReadToEnd)
        sr.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sw As StreamWriter = New StreamWriter(New FileStream("C:\test.ini", FileMode.Create))
        sw.WriteLine(TextBox1.Text)
        sw.Close()
    End Sub
you can add your ini file to the texbox upon clicking button2 then make your changes to the text in the textbox , click button3 and it will update you ini file .
 
Ill try your example, dynamic_sysop.

As for now, I use random acces to read and write from my settings file, in order to store the values in structures for future use.

Is that a good way to store settings ?

______________
SiMoN.
 
I sure have forgotten something.
VB .NET says "Type StreamReader is not defined" and "Type StreamWriter is not defined".

What did I do wrong :eek: ?


__________________
SiMoN.
 
You need to Import the System.IO namespace.

Also, if you wish to store options using an XML file easily, I recommend that you take a look at XML Serialization. You could create a "ProgramSettings" class, serialize it, and deserialize it later. Serializing a class will transfer the structure and data of the class into XML format, and deserializing it will read it back into the class. Its very powerful and easy once you get the hang of it.
 
Thanks guys, this forum is great :)

I managed to do an app that serialized and deserialized a string and an integer - but colors (system.drawing.color) couldnt be serialized!:(

Well, there is a way of storing color in .ini-files - i just translate the color to an integer value, and when reopening it i translate is again to a color. Now, i cant assign an integer to each of the millions of color, but ill be able to store some colors - like ten colors or so.

Is there any way of serializing colors?
 
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c As Color = Color.Blue
        MessageBox.Show(c.R & " " & c.G & " " & c.B)
    End Sub
 
Colors are serializable, so you should have no trouble. Are you remembering to tell the XML Serializer that you want to serialize it? For example, you usually make an array of all the types you want to serialize:

Code:
Dim serTypes() As Type = {GetType(String), GetType(Drawing.Color), GetType(MyClass)}
Dim xmlSer As New Xml.Serialization.XmlSerializer(GetType(Integer), serTypes)
 ^ The first param is the type you wish to serialize, and the second
 param is an array of other types you also wish to serialize. With
 the array above, you can serialize String, Color, MyClass, plus
 Integer.

do the serialization here
 
VB .NET says that:
"There was an error generating the XML document"

when executing the last statement below:

Dim serTypes() As Type = {GetType(String), GetType(Drawing.Color), GetType(test)}

Dim theSerializer As New XmlSerializer(GetType(Integer), serTypes)

Dim theWriter As New StreamWriter("testSerializer.xml", FileMode.Create)

theSerializer.Serialize(theWriter, serTypes)

( i think it looks just fine :confused: )

( sorry for the delay - i check this forum two or three times every day, but sometimes it

doesnt let me post a reply :eek: )
 
Back
Top