INI files in CSharp

D

Dom

I used to love the INI files in VB, even though their use was
deprecated. It was a great way to save application information, like
the last position and size of the main window, and it never taxed the
system the way using the registry might.

Can I get something like GetPrivateProfileString in CSharp?

Dom
 
M

Mark Rae

P

Peter Duniho

Dom said:
I used to love the INI files in VB, even though their use was
deprecated. It was a great way to save application information, like
the last position and size of the main window, and it never taxed the
system the way using the registry might.

Can I get something like GetPrivateProfileString in CSharp?

See the built-in "Settings" functionality. It creates an XML file that does
basically the same thing an INI file did. It's easier to use than the INI
file API too.

Pete
 
D

Dom

Hi Pete;

Can you tell me a little more about the "Settings" function? It seems
to be what I need. What class am I looking at?

All I find is System.XML.XMLDocument and a mess of methods and
properties.

Dom
 
P

Peter Duniho

Dom said:
Hi Pete;

Can you tell me a little more about the "Settings" function? It seems
to be what I need. What class am I looking at?

All I find is System.XML.XMLDocument and a mess of methods and
properties.

Assuming you're using Visual Studio 2005:

In the Solution Explorer, under the project you'll find a Properties
section. Under that is a Settings item. Double-click that to add settings
to your project.

In code, you access the settings via Properties.Settings.Default (don't ask
my why you have to go through "Default"...I assume there's some good reason
related to other things you can do with Settings, but I haven't figured that
out yet :) ). Each setting shows up as a property under the Default
property and you can read and write to the settings just like any other
read/write property.

To make the settings persistent, just call
Properties.Settings.Default.Save() after you've changed them.

That's how you use it. What happens "behind the scenes" is that an XML file
is created under the user's "Documents and Settings" directory (XP, not sure
what happens in Vista but probably similar) in which all of those settings
are actually stored.

Note that the above is for settings with "Scope" of "User". If the "Scope"
is set to "Application", then the settings aren't maintained per-user, and
they aren't changeable by a user.

Pete
 
D

Dom

Thanks, Pete. This is perfect.

Just for the record, (you may already know this), I found that you can
"Add...New Item...Settings" to the project. For some reason, it
doesn't go automatically to the properties folder, but you can move it
there, and rename it to something other than "Settings2.Settings".
Then, if you name it to "DataSources", you access it via
Properties.DataSources.Default

To me, this is useful, since it allows me to organize my data better.

Thanks again,
Dom
 

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