File problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shane Mergy

    #1

    File problems

    i have a program that reads through an ini file and does stuff so far
    the only thing i am having trouble with is the writing to the ini
    file. if the section is alreadey there i want to do a search for that
    line then all text to the next line of end of that block can someone
    help with this.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: File problems

    Shane,

    Are you doing this parsing yourself? Have you considered using the
    WritePrivatePro fileString/WritePrivatePro fileSection API functions? You can
    call these to write your INI file sections, and you can use the
    ReadPrivateProf ileString/ReadPrivateProf ileSection API functions to read the
    same INI file.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Shane Mergy" <mongo@ameritec h.net> wrote in message
    news:41f6b5a9.0 309160456.51501 89c@posting.goo gle.com...[color=blue]
    > i have a program that reads through an ini file and does stuff so far
    > the only thing i am having trouble with is the writing to the ini
    > file. if the section is alreadey there i want to do a search for that
    > line then all text to the next line of end of that block can someone
    > help with this.[/color]


    Comment

    • Kyriakos Stavrou

      #3
      Re: File problems

      You should better use a class specially for settings. This class must be
      Serializable

      This is an exapmle that saves and retrievs the setting for the font size of
      a textBox in a file

      -----------Settings Class---------------
      using System;

      [Serializable]

      public class settingsClass

      {

      public settingsClass(f loat size,int delay)


      {

      fontSize=size;

      programDelay=de lay;

      }

      #region Font Size

      private float fontSize;

      public float FontSize

      {

      get

      {

      return fontSize;

      }

      set

      {

      fontSize=value;

      }

      }

      #endregion Font Size

      }




      -----------loadSaveSetting sClass--------------------------------
      using System.IO;
      using System.Drawing;
      using System.Runtime. Serialization.F ormatters.Binar y;

      public class loadSaveSetting sClass
      {
      public loadSaveSetting sClass(main mf)
      {
      mainForm=mf;
      }

      private static main mainForm;

      private static BinaryFormatter binaryFormatter = new BinaryFormatter ();

      private static FileStream fileStream;
      private static settingsClass settings;

      #endregion ???????? ??????????


      public static void loadSettings()
      {
      settings=new settingsClass(0 ,0);

      string fileName=(strin g)mainForm.Prog ramDirectory+"\ \settings.dat";

      try
      {
      fileStream=new FileStream(file Name,FileMode.O pen,FileAccess. Read);
      settings=(setti ngsClass) binaryFormatter .Deserialize(fi leStream);
      }

      catch (System.IO.File NotFoundExcepti on)
      {
      return;
      }

      catch (System.Runtime .Serialization. SerializationEx ception)
      {
      fileStream.Clos e();
      return;
      }

      fileStream.Clos e();

      mainForm.source Code.Font=new
      Font(mainForm.s ourceCode.Font. FontFamily,sett ings.FontSize);
      }

      public static void saveSettings()
      {
      settings = new
      settingsClass(m ainForm.sourceC ode.Font.Size,m ainForm.delayTr ackBar.Value);

      string fileName=(strin g)mainForm.Prog ramDirectory+"\ \settings.dat";

      fileStream=new
      FileStream(file Name,FileMode.O penOrCreate,Fil eAccess.Write);

      binaryFormatter .Serialize(file Stream,settings );

      fileStream.Clos e();
      }
      #endregion Save Setting
      }


      "Shane Mergy" <mongo@ameritec h.net> wrote in message
      news:41f6b5a9.0 309160456.51501 89c@posting.goo gle.com...[color=blue]
      > i have a program that reads through an ini file and does stuff so far
      > the only thing i am having trouble with is the writing to the ini
      > file. if the section is alreadey there i want to do a search for that
      > line then all text to the next line of end of that block can someone
      > help with this.[/color]


      Comment

      • Shane Mergy

        #4
        Re: File problems



        how would i do this


        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: File problems

          Shane,

          Here is the declaration for WritePrivatePro fileString:

          [DllImport("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
          public static extern bool WritePrivatePro fileString(
          [MarshalAs(Unman agedType.LPTStr )] string lpAppName,
          [MarshalAs(Unman agedType.LPTStr )] string lpKeyName,
          [MarshalAs(Unman agedType.LPTStr )] string lpString,
          [MarshalAs(Unman agedType.LPTStr )] string lpFileName);

          And here is the declaration for WritePrivatePro fileSection:

          [DllImport("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
          public static extern bool WritePrivatePro fileSection(
          [MarshalAs(Unman agedType.LPTStr )] string lpAppName,
          [MarshalAs(Unman agedType.LPTStr )] string lpString,
          [MarshalAs(Unman agedType.LPTStr )] string lpFileName);

          You will have to have the "using System.Runtime. InteropServices ;"
          directive at the top of the file as well.

          Hope this helps.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - nicholas.paldin o@exisconsultin g.com

          "Shane Mergy" <mongo@ameritec h.net> wrote in message
          news:%23wt33yFf DHA.3788@tk2msf tngp13.phx.gbl. ..[color=blue]
          >
          >
          > how would i do this
          >
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]


          Comment

          Working...