ssdjgru1
asked on
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 ?
Can you explain bit more? What do you mean by random line?
ASKER
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
COMPAR = 1
COMSPEED = 19200
FILEMODE = APPEND
...
Now I would like to change the for example COMSPEED = 19200 to COMSPEED = 57000
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?
ASKER
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.
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
http://msdn.microsoft.com/library/en-us/dnbda/html/cmab.asp
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.InteropServ ices;
namespace INIReader
{
public class INIReader
{
[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProf ileIntA",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileInt(strin g lpApplicationName,
string lpKeyName, int nDefault, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint="WritePrivatePr ofileStrin gA",
CharSet=CharSet.Ansi)]
private static extern int WritePrivateProfileString (string
lpApplicationName, string lpKeyName, string lpString, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProf ileStringA ",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileString (string lpApplicationName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
nSize, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProf ileSection NamesA",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileSectionNa mes (byte[]
lpszReturnBuffer, int nSize, string lpFileName);
[DllImport("KERNEL32.DLL", EntryPoint="WritePrivatePr ofileSecti onA",
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(secti on, 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(se ction, 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(sect ion, 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(R eadString( 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(s ection, 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(val ue, 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];
GetPrivateProfileSectionNa mes(buffer , MAX_ENTRY, Filename);
string [] parts = Encoding.ASCII.GetString(b uffer).Tri m('\0').Sp lit('\0');
return new ArrayList(parts);
}
catch {}
return null;
}
private string m_Filename;
private string m_Section;
private const int MAX_ENTRY = 32768;
}
}
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.InteropServ
namespace INIReader
{
public class INIReader
{
[DllImport("KERNEL32.DLL",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileInt(strin
string lpKeyName, int nDefault, string lpFileName);
[DllImport("KERNEL32.DLL",
CharSet=CharSet.Ansi)]
private static extern int WritePrivateProfileString (string
lpApplicationName, string lpKeyName, string lpString, string lpFileName);
[DllImport("KERNEL32.DLL",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileString (string lpApplicationName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
nSize, string lpFileName);
[DllImport("KERNEL32.DLL",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileSectionNa
lpszReturnBuffer, int nSize, string lpFileName);
[DllImport("KERNEL32.DLL",
CharSet=CharSet.Ansi)]
private static extern int WritePrivateProfileSection
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(secti
}
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(se
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(sect
}
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(R
}
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(s
}
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
}
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(val
}
}
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
}
public bool DeleteKey(string key)
{
return (WritePrivateProfileString
}
public bool DeleteSection(string section)
{
return WritePrivateProfileSection
}
public ArrayList GetSectionNames()
{
try
{
byte[] buffer = new byte[MAX_ENTRY];
GetPrivateProfileSectionNa
string [] parts = Encoding.ASCII.GetString(b
return new ArrayList(parts);
}
catch {}
return null;
}
private string m_Filename;
private string m_Section;
private const int MAX_ENTRY = 32768;
}
}
Ahh yes... the notorious GetPrivateProfileStringA.. .
my favorite. =]]]
my favorite. =]]]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.