[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

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 ?
0
ssdjgru1
Asked:
ssdjgru1
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now