CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2004
    Posts
    346

    How do i do ini file in C#.Net ?

    in Visual C++ i had :

    Set/GetPrivateProfileString , GetPrivateProfileint and more , creating a file with a label and a tag for each variable like :

    [House]
    chairs=5
    doors=7
    [City]
    hospitals=2
    airport=1.

    Is there a better way to save values of variables when the application is closed and get them when the application starts ?

    and if not , how do i make an ini file in C# ?

  2. #2
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Re: How do i do ini file in C#.Net ?

    Use XML file and take advantage of .NET XML classes for manipulation
    "We act as though comfort and luxury were the chief requirements of
    life, when all that we need to make us happy is something to be
    enthusiastic about."

    - Einstein

  3. #3
    Join Date
    Aug 2003
    Location
    London
    Posts
    515

    Re: How do i do ini file in C#.Net ?

    Yeah, should use XML where you can.

    Pretty sure this is a link to a nice library that allows XML, INI, Registry etc editing (Couldn't confirm the link, the site is playing up..!)

    http://www.codeproject.com/csharp/ReadWriteXmlIni.asp

    Tidy bit of code..!
    If it helped, then please rate the post by clicking "Rate this post"!

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

  5. #5
    Join Date
    Aug 2004
    Posts
    346

    Re: How do i do ini file in C#.Net ?

    thanks guys!

  6. #6
    Join Date
    Apr 2003
    Posts
    108

    Re: How do i do ini file in C#.Net ?

    Hello,
    I am trying to use nini for ini files. Just one problem:

    When trying to read non existing section/key, there is this error message "NullReferenceException was unhandled".

    Do you know how to proceed to avoid this ?

    Regards.
    Last edited by efkefk; August 7th, 2006 at 09:35 AM.

  7. #7
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: How do i do ini file in C#.Net ?

    Put try...catch surrounding the problematic code.


    Or you could try to fix the bug (it's an open source with full source code)

  8. #8
    Join Date
    Apr 2003
    Posts
    108

    Re: How do i do ini file in C#.Net ?

    " try ... catch" works well.

    Thank you.

  9. #9
    Join Date
    Aug 2004
    Posts
    346

    Re: How do i do ini file in C#.Net ?

    Why don't you catch the exception and do what you want with it ( could do nothing if you like) ?
    try
    {

    // read and unexisting section - that will throw an exception

    }
    catch (Exception) // catch any exception thrown
    {
    // do something or nothing
    }

  10. #10
    Join Date
    Apr 2003
    Posts
    108

    Re: How do i do ini file in C#.Net ?

    Yes, I did that.
    This works well.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured