Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

SiMoN.
  • *Experts*
Posted

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.

Posted

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.

SiMoN.
Posted

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.

SiMoN.
  • Leaders
Posted

   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 .

  • 1 month later...
Posted

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.

SiMoN.
Posted

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.

SiMoN.
  • *Experts*
Posted

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.

Posted

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?

SiMoN.
  • Leaders
Posted
   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

  • *Experts*
Posted

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

Posted

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: )

SiMoN.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...