Read INIFile from String or Stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2lwcHl1Y29ubg==?=

    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 WritePrivatePro fileString and
    GetPrivateProfi leString. Is there a way around this instead of writing my own
    class to operate on a string or stream ???

    Thanks
  • =?Utf-8?B?RnJhbmsgVXJheQ==?=

    #2
    RE: Read INIFile from String or Stream

    Hi

    Maybe you can use this:

    System.IO.Strea mReader local_StreamRea der = new
    System.IO.Strea mReader("FilePa th");
    string local_text = local_StreamRea der.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 WritePrivatePro fileString and
    GetPrivateProfi leString. Is there a way around this instead of writing my own
    class to operate on a string or stream ???
    >
    Thanks

    Comment

    • Zhi-xin Ye

      #3
      RE: Read INIFile from String or Stream

      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 GetPrivateProfi leString 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.Sel ectSingleNode() 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(o bject 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.Loa dXml(settings);

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

      string xpath = string.Format(
      "/Sections/Section[@name=\"{0}\"]/key[@name=\"{1}\"]",
      section, key);
      string Value = settingsXml.Sel ectSingleNode(
      xpath).Attribut es["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



      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:
      msdnmg@microsof t.com.

      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Gain technical skills through documentation and training, earn certifications and connect with the community

      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.

      Comment

      Working...