VB icon

IniFile class

Email
Submitted on: 8/30/2016 2:23:15 PM
By: Thomas1965  
Level: Beginner
User Rating: Unrated
Compatibility: C#
Views: 1548
 
     Simple Wrapper around GetPrivateProfileString and WritePrivateProfileString to read and write .ini files. Can read and write int, float, double, decimal, bool and string.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: IniFile class
// Description:Simple Wrapper around GetPrivateProfileString and WritePrivateProfileString to read and write .ini files. Can read and write int, float, double, decimal, bool and string.
// By: Thomas1965
//**************************************

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Windows.Forms;
public class IniFile
{
 private string FileName;
#region Constructors
 public IniFile(string filename)
 {
FileName = filename;
 }
 public IniFile()
 {
FileName = Path.ChangeExtension(Application.ExecutablePath, ".ini");
 }
#endregion
#region Read functions
 public string ReadString(string section, string ident, string defvalue)
 {
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, ident, defvalue, sb, 1024, FileName);
return sb.ToString();
 }
 public int ReadInt(string section, string ident, int defvalue)
 {
string erg = ReadString(section, ident, "");
try
{
 return int.Parse(erg);
}
catch
{
 return defvalue;
}
 }
 public bool ReadBool(string section, string ident, bool defvalue)
 {
string erg = ReadString(section, ident, "");
try
{
 return bool.Parse(erg);
}
catch
{
 return defvalue;
}
 }
 #endregion
#region Write functions
 public void WriteString(string section, string ident, string value)
 {
if (!WritePrivateProfileString(section, ident, value, FileName))
 throw new Exception("Error writing in " + FileName);
 }
 public void WriteBool(string section, string ident, bool value)
 {
WriteString(section, ident, value.ToString());
 }
 public void WriteInt(string section, string ident, int value)
 {
WriteString(section, ident, value.ToString());
 }
 public void WriteFloat(string section, string ident, float value)
 {
WriteString(section, ident, value.ToString());
 }
 public void WriteDouble(string section, string ident, double value)
 {
WriteString(section, ident, value.ToString());
 }
 public void WriteDecimal(string section, string ident, decimal value)
 {
WriteString(section, ident, value.ToString());
 }
 
 
 public float ReadFloat(string section, string ident, float defvalue)
 {
string erg = ReadString(section, ident, "");
try
{
 return float.Parse(erg);
}
catch
{
 return defvalue;
}
 }
 public double ReadDouble(string section, string ident, double defvalue)
 {
string erg = ReadString(section, ident, "");
try
{
 return double.Parse(erg);
}
catch
{
 return defvalue;
}
 }
 public decimal ReadDecimal(string section, string ident, decimal defvalue)
 {
string erg = ReadString(section, ident, "");
try
{
 return decimal.Parse(erg);
}
catch
{
 return defvalue;
}
 }
 #endregion
#region InterOp
 [DllImport("Kernel32.dll")]
 private static extern bool WritePrivateProfileString(string Section, string key,
string value, string filename);
 [DllImport("Kernel32.dll")]
 private static extern UInt32 GetPrivateProfileString(string Section, string key,
string defvalue, StringBuilder buffer,
UInt32 bufsize, string filename);
#endregion
}	


Other 21 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.