Using an xml file in place of a .ini

W

Woody Splawn

In the tool I used to use I would write certain things to a .ini file in a
users private directory from time to time. For example, when the
application was started, a certain section of that .ini file is checked so
that the application will start in a way that is unique to the users
machine. This is a non windows app.

I see that it is not convenient to do the same in VB.net. Tools for writing
and reading from a .ini file are not good. It has occurred to me, however,
that I might just as easily make use of writing that data to an xml file and
get the same result.

Is anyone else doing it this way?
 
G

Greg Burns

From what I've read, the registry is still your best option (though not the
..NET way).

check out this month MSDN magazine's "User Preferences" for some other
ideas:
http://msdn.microsoft.com/msdnmag/issues/04/07/default.aspx

A quick and dirty way is to create a simple class with your user's settings
and serialize it to their profile folder on disk. Here is the code I having
been toying around with:

<Serializable()> _
Public Class UserSettings
Private _username As String

Public Property UserName() As String
Get
If _username = Nothing Then
Return CurrentUser()
Else
Return _username.ToLower
End If
End Get
Set(ByVal Value As String)
_username = Value.ToLower
End Set
End Property


End Class

Imports System.Runtime.Serialization.Formatters.Binary

Public Class UserSettingsDB
Private Sub New()
End Sub

Public Shared Function LoadSettings() As UserSettings

Dim mySettings As New UserSettings

Dim s As String =
Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)

s &= "\myappname\myappname.bin"

If File.Exists(s) Then

Dim streamOut As FileStream


Try
Dim formatter As New BinaryFormatter



streamOut = New FileStream(s, FileMode.Open,
FileAccess.Read)

mySettings = CType(formatter.Deserialize(streamOut),
UserSettings)

Catch

Finally
streamOut.Close()
End Try
End If

Return mySettings

End Function

Public Shared Sub SaveSettings(ByVal mySettings As UserSettings)
Dim s As String =
Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)

s &= "\myappname"

If Not Directory.Exists(s) Then

Directory.CreateDirectory(s)

End If

s &= "\myappname.bin"

Dim formatter As New BinaryFormatter

Dim streamIn As New FileStream(s, FileMode.Create,
FileAccess.Write)

formatter.Serialize(streamIn, mySettings)

streamIn.Close()

End Sub
End Class

HTH,
Greg
 
H

Herfried K. Wagner [MVP]

* "Woody Splawn said:
In the tool I used to use I would write certain things to a .ini file in a
users private directory from time to time. For example, when the
application was started, a certain section of that .ini file is checked so
that the application will start in a way that is unique to the users
machine. This is a non windows app.

Configuration Management Application Block
<URL:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/cmab.asp>

<URL:http://www.palmbytes.de/content/dotnet/optionslib.htm>
 
O

One Handed Man \( OHM#\)

One way is to save your stuff to a table inside a dataset, and then simply
use the datasets writeXml & readXml methods. However, this could have
security issues dependent on your congiguration. The registry potentially
allows a more secure method of storage but can be a little slower so Im
informed although Ive never tested that out.

Cheers - OHM
 
J

JLW

Go to my website, and send me an e-mail. I have a library that will do
that, and you can have seperate files for each profile, or contain it all
inside an app.config file. Your preference. It acts just like an INI file
without the hassle

HTH,
JLW
 
C

Cor Ligthert

Hi Woody,

Yes, I prefer the same way as OHM, except that in my experience the registry
is the fastest.

Registry for things that are typical workstation depended.
- Last place of a form
- Size of the form
- encrypted passwords
Etc.

XML file for installation dependable things
- Not good fitting in the resource files language's issues (I thought you
had some Spanish issues, however maybe I mix you up with someone else)
- Special user settings before configuration etc.
- All things you want to take with you when your workstation is crashed,
however you have a backup.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top