473,511 Members | 15,364 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read INIFile from String or Stream

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
Jul 7 '08 #1
2 5303
Hi

Maybe you can use this:

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

"sippyuconn" wrote:
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
Jul 8 '08 #2
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7343
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
3
2738
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
2
12318
by: Just D. | last post by:
All! How should we read any file from some URL? I found in MSDN the method URLDownloadToFile function, but it's for C#. There is another example to read but it doesn't work if the page is more...
11
2383
by: Harry Whitehouse | last post by:
I'm trying to obtain the load directory for my application, as well as the Windows directory on the machine, but I don't know how this is accomplished in VC++ .NET. In C# there is an Application...
3
4934
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
3
1352
by: markclinn | last post by:
I have a device in the field that I access by the stream method. I open the Stream and do the following: 1. stream.write a character to the device. 2. stream.read the information from the...
14
2703
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
2
6599
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
0
2862
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.