insert line in text file

I am working on settings part of my program. I have some check buttons and stuff, and now I would like to write to settings.ini file at random line. How could that be done ?
ssdjgru1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chester_M_RagelCommented:
Can you explain bit more? What do you mean by random line?
0
ssdjgru1Author Commented:
I have text file SETTINGS.INI with the following content:

COMPAR = 1
COMSPEED = 19200
FILEMODE = APPEND
...

Now I would like to change the for example COMSPEED = 19200 to COMSPEED = 57000
0
Chester_M_RagelCommented:
The better choice is to use xml file to do this kind of works. If you are going for a text file(like this) read the line by line using StreamReader get the string and split it as you think and set the new value  or replace it with the new one.. But its not the better or even good one. Why don't you use xml configuration files to do this?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ssdjgru1Author Commented:
I've read in some article, that the application configuration files can only be read and must be written manualy. Correct me if I am wrong.
0
Chester_M_RagelCommented:
Normally if you are using App.???.config and accessing that using Configuration of .NET then that is true. But you can use plain xml file to keep your configuration. This may be useful for you...

http://msdn.microsoft.com/library/en-us/dnbda/html/cmab.asp
0
SRigneyCommented:
You have a couple of options.  An easy way is to build a DataSet and just do WriteXML to save to an XML file and ReadXML to read the xml file.  You'll see lots of arguments against this for speed and resources, but for simplicity, this has been the absolutely easiest way that I have found.

You mentioned specifically using .INI files with c#.  That is a possibility.  You can use the actual API calls and get data from the .ini file.  In that case the Key system .dll would handle the location of writing the data for you.


Here is an .INI file class.

using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;

namespace INIReader
{
      public class INIReader
      {
            [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileIntA",
                   CharSet=CharSet.Ansi)]
            private static extern int GetPrivateProfileInt(string lpApplicationName,
                  string lpKeyName, int nDefault, string lpFileName);

            [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringA",
                   CharSet=CharSet.Ansi)]
            private static extern int WritePrivateProfileString (string
                  lpApplicationName, string lpKeyName, string lpString, string lpFileName);

            [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA",
                   CharSet=CharSet.Ansi)]
            private static extern int GetPrivateProfileString (string lpApplicationName,
                  string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
                  nSize, string lpFileName);

            [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileSectionNamesA",
                   CharSet=CharSet.Ansi)]
            private static extern int GetPrivateProfileSectionNames (byte[]
                  lpszReturnBuffer, int nSize, string lpFileName);

            [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileSectionA",
                   CharSet=CharSet.Ansi)]
            private static extern int WritePrivateProfileSection (string lpAppName,
                  string lpString, string lpFileName);

            public INIReader(string file)
            {
                  Filename = file;
            }

            public string Filename
            {
                  get
                  {
                        return m_Filename;
                  }
                  set
                  {
                        m_Filename = value;
                  }
            }

            public string Section
            {
                  get
                  {
                        return m_Section;
                  }
                  set
                  {
                        m_Section = value;
                  }
            }

            public int ReadInteger(string section, string key, int defVal)
            {
                  return GetPrivateProfileInt(section, key, defVal, Filename);
            }

            public int ReadInteger(string section, string key)
            {
                  return ReadInteger(section, key, 0);
            }

            public int ReadInteger(string key, int defVal)
            {
                  return ReadInteger(Section, key, defVal);
            }

            public int ReadInteger(string key)
            {
                  return ReadInteger(key, 0);
            }

            public string ReadString(string section, string key, string defVal)
            {
                  StringBuilder sb = new StringBuilder(MAX_ENTRY);

                  int Ret = GetPrivateProfileString(section, key, defVal, sb, MAX_ENTRY,
                        Filename);

                  return sb.ToString();
            }

            public string ReadString(string section, string key)
            {
                  return ReadString(section, key, "");
            }

            public string ReadString(string key)
            {
                  return ReadString(Section, key);
            }

            public long ReadLong(string section, string key, long defVal)
            {
                  return long.Parse(ReadString(section, key, defVal.ToString()));
            }

            public long ReadLong(string section, string key)
            {
                  return ReadLong(section, key, 0);
            }

            public long ReadLong(string key, long defVal)
            {
                  return ReadLong(Section, key, defVal);
            }

            public long ReadLong(string key)
            {
                  return ReadLong(key, 0);
            }

            public byte[] ReadByteArray(string section, string key)
            {
                  try
                  {
                        return Convert.FromBase64String(ReadString(section, key));
                  }
                  catch {}

                  return null;
            }

            public byte[] ReadByteArray(string key)
            {
                  return ReadByteArray(Section, key);
            }

            public bool ReadBoolean(string section, string key, bool defVal)
            {
                  return Boolean.Parse(ReadString(section, key, defVal.ToString()));
            }

            public bool ReadBoolean(string section, string key)
            {
                  return ReadBoolean(section, key, false);
            }

            public bool ReadBoolean(string key, bool defVal)
            {
                  return ReadBoolean(Section, key, defVal);
            }

            public bool ReadBoolean(string key)
            {
                  return ReadBoolean(Section, key);
            }

            public bool Write(string section, string key, int value)
            {
                  return Write(section, key, value.ToString());
            }

            public bool Write(string key, int value)
            {
                  return Write(Section, key, value);
            }

            public bool Write(string section, string key, string value)
            {
                  return (WritePrivateProfileString(section, key, value, Filename) != 0);
            }

            public bool Write(string key, string value)
            {
                  return Write(Section, key, value);
            }

            public bool Write(string section, string key, long value)
            {
                  return Write(section, key, value.ToString());
            }

            public bool Write(string key, long value)
            {
                  return Write(Section, key, value);
            }

            public bool Write(string section, string key, byte [] value)
            {
                  if (value == null)
                  {
                        return Write(section, key, (string)null);
                  }
                  else
                  {
                        return Write(section, key, value, 0, value.Length);
                  }
            }

            public bool Write(string key, byte [] value)
            {
                  return Write(Section, key, value);
            }

            public bool Write(string section, string key, byte [] value, int offset, int length)
            {
                  if (value == null)
                  {
                        return Write(section, key, (string)null);
                  }
                  else
                  {
                        return Write(section, key, Convert.ToBase64String(value, offset, length));
                  }
            }

            public bool Write(string section, string key, bool value)
            {
                  return Write(section, key, value.ToString());
            }

            public bool Write(string key, bool value)
            {
                  return Write(Section, key, value);
            }

            public bool DeleteKey(string section, string key)
            {
                  return (WritePrivateProfileString(section, key, null, Filename) != 0);
            }

            public bool DeleteKey(string key)
            {
                  return (WritePrivateProfileString(Section, key, null, Filename) != 0);
            }

            public bool DeleteSection(string section)
            {
                  return WritePrivateProfileSection(section, null, Filename) != 0;
            }

            public ArrayList GetSectionNames()
            {
                  try
                  {
                        byte[] buffer = new byte[MAX_ENTRY];
                        GetPrivateProfileSectionNames(buffer, MAX_ENTRY, Filename);
                        string [] parts = Encoding.ASCII.GetString(buffer).Trim('\0').Split('\0');
                        return new ArrayList(parts);
                  }
                  catch {}
                  return null;
            }

            private string m_Filename;
            private string m_Section;
            private const int MAX_ENTRY = 32768;
      }
}
0
gregasmCommented:
Ahh yes... the notorious GetPrivateProfileStringA...

my favorite. =]]]
0
SRigneyCommented:
I posted an entire class to read .ini files that solved the problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.