Simon Posted June 8, 2003 Posted June 8, 2003 Hello, For an application that I'm 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. Quote SiMoN.
*Experts* Volte Posted June 8, 2003 *Experts* Posted June 8, 2003 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. Quote
aewarnick Posted June 8, 2003 Posted June 8, 2003 I have found the registry is easiest to store settings. Although it is easiest I would say that XML is best. Quote C#
Simon Posted June 8, 2003 Author Posted June 8, 2003 Ooooo blimey ... lol :) Okay, I'm 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! I'm stunned by the possibilities of .NET and how complex programming can be, now, this is gonna be a challenge for me. Once I've understood I'll be very happy and that is, in my opinion, the most wonderful part of programming - to finally understand. Thanks, SiMoN. Quote SiMoN.
Simon Posted June 9, 2003 Author Posted June 9, 2003 Hi folks What is the advatage of using XML instead of .ini? It just seems more complicated:eek: Im sorry, the msdn guides don't help at all:( bye, SiMoN. Quote SiMoN.
aewarnick Posted June 9, 2003 Posted June 9, 2003 Is is rough. Maybe try using the registry instead. Quote C#
Leaders dynamic_sysop Posted June 13, 2003 Leaders Posted June 13, 2003 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 . Quote
Simon Posted July 17, 2003 Author Posted July 17, 2003 I'll 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. Quote SiMoN.
Simon Posted July 21, 2003 Author Posted July 21, 2003 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. Quote SiMoN.
*Experts* Volte Posted July 21, 2003 *Experts* Posted July 21, 2003 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. It's very powerful and easy once you get the hang of it. Quote
Leaders dynamic_sysop Posted July 21, 2003 Leaders Posted July 21, 2003 if you wish to read from .ini files you still can you know :) i could bung you an example together. Quote
Simon Posted July 22, 2003 Author Posted July 22, 2003 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) couldn't 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 can't assign an integer to each of the millions of color, but i'll be able to store some colors - like ten colors or so. Is there any way of serializing colors? Quote SiMoN.
Heiko Posted July 22, 2003 Posted July 22, 2003 There used to be a RGB function. Is it still around? Quote .nerd
Leaders dynamic_sysop Posted July 22, 2003 Leaders Posted July 22, 2003 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 Quote
*Experts* Volte Posted July 22, 2003 *Experts* Posted July 22, 2003 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: 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 Quote
Simon Posted July 23, 2003 Author Posted July 23, 2003 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 doesn't let me post a reply :eek: ) Quote SiMoN.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.