.NET version of INI files ?

  • Thread starter Thread starter Herby
  • Start date Start date
H

Herby

Im rewriting a current MFC application for .NET using C++\CLI that
compiles \clr SAFE.

My application needs to read application settings, it used to do this
from the old application ini file via GetProfileString etc.

What is a direct .NET equivalent of this?

Generally it only needs to read custom properties etc that have been
pre-set.

I guess its somewhere off System::Configuration ???

I found this article
http://www.codeproject.com/csharp/ReadWriteXmlIni.asp

Im just surprised someone should have to write classes to achieve this.
 
Herby said:
Im rewriting a current MFC application for .NET using C++\CLI that
compiles \clr SAFE.

My application needs to read application settings, it used to do this
from the old application ini file via GetProfileString etc.

Well, you could call this API via platform invoke (or IJW), but INI
files are not the .NET way to do things.
What is a direct .NET equivalent of this?

The config file. These are application domain based and they are
associated with an application, (ie not a DLL). When an application
domain is started (there will be at least one for each process instance)
the runtime will create an in-memory cache. When you (or runtime
classes) read a configuration section it will be read from file and then
cached in memory, so subsequent reads of the section will use the cached
value. The section will be read from global configuration file
machine.config and the application config file. The two are merged and
values in the latter take precedence over the former. The application
configuration file has the name of the process with the .config suffix
(eg myapp.exe.config). Configuration files are XML files. You can create
your own sections but the schema allows you to use <appSettings> for
simple settings:

http://msdn.microsoft.com/library/en-us/cpgenref/html/gngrfAppSettingsElement.asp

This element is a collection of <add> elements where each one has key
and value attributes. You can use the AppSettingsReader class to read
these values:

http://msdn.microsoft.com/library/e...mConfigurationAppSettingsReaderClassTopic.asp

Gall GetValue with the key name and the type of the value attribute to
get the value.
Generally it only needs to read custom properties etc that have been
pre-set.

The configuartion file is ideal for this because it will read the values
*once* from the configuration file.
I guess its somewhere off System::Configuration ???

I found this article
http://www.codeproject.com/csharp/ReadWriteXmlIni.asp

Im just surprised someone should have to write classes to achieve
this.

Well, Microsoft insists on putting XML *everywhere* and so INI files had
to be junked because they did not contain XML. By the way, config files
are not real XML files, because they do not implement the complete DOM.
The reason is that they are not read by the System.Xml classes they are
read by lightweight classes that only read a subset of XML.

Richard
 
Thanks Richard, very informative, lots of reading and catching up to do
there.
I could not use "IJW" because my application is to run in a highly
secure setting and so must compile as /safe.
I assume reading custom application settings from a .config file is not
in anyway a security issue?
 
The config file.
... The application
configuration file has the name of the process with the .config suffix
(eg myapp.exe.config). Configuration files are XML files.

If memory serves, Richard, you wrote a sample program that illustrates
use of .config files. I remember using the built-in functions for
access to them, but couldn't figure out why settings were getting
overwritten. Turned out that Visual Studio was copying over the
modified file. This occurred with your sample code as well.

When run outside the debugger all was well.
Well, Microsoft insists on putting XML *everywhere* and so INI files had
to be junked because they did not contain XML.

Haha! That does seem to be the case sometimes.

I'm at least hoping that registry abuse tapers down. That's probably
the biggest single reason people have to reinstall XP every couple
years. I've seen a spyware checker (Spywareblaster maybe?) that
copied the entire 'favorites' folder into the registry. On some
machines that's 10,000 new registry keys. Most often, registry pigs
don't clean up after themselves either.
 
Back
Top