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