Read INIFile from String or Stream

S

sippyuconn

Hi

Is there any way to Read an INIFile from a string or Stream instead of a
physical file ???

I want to read the INIFile into a string then store in a db but when I read
the string from the db or save string to the db I don't want to have to copy
the string to a file to use the WritePrivateProfileString and
GetPrivateProfileString. Is there a way around this instead of writing my own
class to operate on a string or stream ???

Thanks
 
F

Frank Uray

Hi

Maybe you can use this:

System.IO.StreamReader local_StreamReader = new
System.IO.StreamReader("FilePath");
string local_text = local_StreamReader.ReadToEnd();


Regards
Frank
 
Z

Zhi-xin Ye

Dear Sippyucoonn,

As I understand, you store the content of the .ini file as a string in the
database, you want to read a specified key value from the string directly
afterwards.
If I misunderstand you, please feel free to let me know.

As far as I know there's no directly way to retrieve the key value from a
string or stream, we have to do some string operation, and it's complex to
perform the string operations if the content of the .ini file is very
large. I think you want to avoid this.

However, we have alternative ways to do this:

1. Store the data from the .ini file to a database in a more structural
way.

For example, we can:

1). Create a table with three fields: Section, Key and Value;
2). Call the GetPrivateProfileString API to read all the section,
key and value information into this table;
3). Query the table for the setting information.

2. Use an XML file instead of .ini file to store the settings.

It's more convenience to retrieve a certain value from XML structures
than plain strings.

For exmple, we have the following content in an .ini file:

[Section1]
key1=value1
key2=value2

[Section2]
key3=value3

We can create an XML file to store the content instead in the following
format:

<Sections>
<Section name="Section1">
<key name="key1" value="value1"/>
<key name="key2" value="value2"/>
</Section>
<Secion name="Section2">
<key name="key3" value="value3"/>
</Section>
</Sections>

Then we can store the whole content of the XML file as a single string
into the database.

Afterwards, we can take the following steps to retrieve a specify
setting entry.

1). Read the setting string from the database using SqlDataReader;
2). Create an XmlDocument object;
3). Load the setting information into the XmlDocument object by calling
the LoadXml() method;
4). Call the XmlDocument.SelectSingleNode() method to get the entry we
need;
5). Read the value of the "value" attribute to get the value for the
specify key.

The following sample demonstrates how to do this:

private void button1_Click(object sender, EventArgs e)
{
//read the xml string from the database
//I just assign a specified value for example
string settings = "<Sections>" +
"<Section name=\"Section1\">" +
"<key name=\"key1\" value=\"value1\"/>" +
"<key name=\"key2\" value=\"value2\"/>" +
"</Section>" +
"<Section name=\"Section2\">" +
"<key name=\"key3\" value=\"value3\"/>" +
"</Section>" +
"</Sections>";

XmlDocument settingsXml = new XmlDocument();
settingsXml.LoadXml(settings);

string section = "Section1";
string key = "key2";

string xpath = string.Format(
"/Sections/Section[@name=\"{0}\"]/key[@name=\"{1}\"]",
section, key);
string Value = settingsXml.SelectSingleNode(
xpath).Attributes["value"].Value;

MessageBox.Show(Value);
}

3. Use the Application Configuration File.

This is the recommended way in .NET for storing configuration
settings.

You can read the following article to get more information about the
Application Configuration.

Configuration Settings File for providing application configuration
data
http://www.codeproject.com/KB/dotnet/config.aspx


Please don't hesitate to contact me if you have any concerns.


Sincerely,
Zhi-Xin Ye
Microsoft Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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