473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to work with .INI-files from C# ?

Are there good classes to work with .INI-files from C# ?

Oleg Subachev
Apr 24 '06 #1
5 10563
theres some freebies about - heres one to get you started
http://www.mentalis.org/soft/class.qpx?id=6

--
Regards

John Timney
Microsoft MVP

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
Are there good classes to work with .INI-files from C# ?

Oleg Subachev

Apr 24 '06 #2
On 24/04/2006 Oleg Subachev wrote:
Are there good classes to work with .INI-files from C# ?

Oleg Subachev


No!!!

You probably ought to think about XML files now but I use the following
for ini files:

/// <summary>Returns the value of a key in a section of an INI file
/// </summary>
/// <param name="strSection">String containing section name</param>
/// <param name="strKeyName">String containing key name</param>
/// <param name="strFileName">Full path and file name of INI
file</param>
public static string INIGetKeyValue(string strSection, string
strKeyName, string strFileName)
{
int intReturn = 0;
int BufferSize = 2048;

StringBuilder lpReturn = new StringBuilder(BufferSize);

intReturn = GetPrivateProfileString(strSection, strKeyName, "",
lpReturn, BufferSize, strFileName);
if (intReturn == 0)
{
return "";
}

return lpReturn.ToString();
}

/// <summary>Writes a key/value pair to a section of an INI file
/// </summary>
/// <param name="strSection">String containing section name</param>
/// <param name="strKeyName">String containing key name</param>
/// <param name="strKeyValue">String containing key value</param>
/// <param name="strFileName">Full path and file name of INI
file</param>
public static bool INIWriteKey(string strSection, string strKeyName,
string strKeyValue, string strFileName)
{
int intReturn = WritePrivateProfileString(strSection, strKeyName,
strKeyValue, strFileName);

if (intReturn == 0)
return false;
else
return true;
}

// GetPrivateProfileString
[DllImport("kernel32.DLL", EntryPoint = "GetPrivateProfileString",
SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetPrivateProfileString(string
lpSectionName, string lpKeyName, string lpDefault, StringBuilder
lpReturnedString, int nSize, string lpFileName);

// WritePrivateProfileString
[DllImport("kernel32.DLL", EntryPoint = "WritePrivateProfileStringA",
SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public static extern int WritePrivateProfileString(string
lpSectionName, string lpKeyName, string lpKeyValue, string lpFileName);
--
Jeff Gaines - Damerham Hampshire UK
Using XanaNews 1.18.1.3
Apr 24 '06 #3
"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
Are there good classes to work with .INI-files from C# ?


Nothing native to the Framework, because that's not the way the Framework
wants you to work these days...

For WinForms apps, use app.config and for WebForms apps use web.config
instead.
Apr 24 '06 #4

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
Are there good classes to work with .INI-files from C# ?

Oleg Subachev


I had a legacy app I was converting and needed to be able to read ini files.
I created a little app state dll that I've been able to reuse. Here's the
main ini class that reads / writes to ini files. (Beware of word wrap!).

Robert

using System;
using System.Xml;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text;
namespace SNCIT2.AppState
{

public class Ini
{

#region DllImports

[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileIntA")]
static extern public int GetPrivateProfileInt(string sectionName,
string keyName, int defaultVal, string fileName);
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringA",
SetLastError = true)]
static extern public int GetPrivateProfileString(string sectionName,
string keyName, string defaultVal, StringBuilder returnVal, int returnSize,
string fileName);
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringA")]
static extern public int WritePrivateProfileString(string sectionName,
string keyName, string keyVal, string fileName);

[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringA")]
static extern public int DeleteKeyValue(string sectionName, string
keyName, string keyVal, string fileName);
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringA")]
static extern public int GetINIKeys(string sectionName, string
keyName, string defaultVal, string returnVal, int returnSize, string
fileName);
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileStringA")]
static extern public int GetINISections(string sectionName, string
keyName, string defaultVal, string returnVal, int returnSize, string
fileName);

#endregion

#region Class Variables

/************************************************** *********************/
/* VARIABLES
*/
/************************************************** *********************/
private struct myVarsStruct
{
public string iniFileName;
public string section;
}

private myVarsStruct myVars = new myVarsStruct();

#endregion

#region Properties

#region IniFileName
/************************************************** *********************/
/* PROPERTIES
*/
/************************************************** *********************/
///************************************************** **********************
/// <summary>
/// IniFileName
/// </summary>
/// <returns>
/// The property IniFileName returns a data type of string.
/// </returns>
/// <revisionhistory>
/// 3/29/2006 12:00:00 AM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public string IniFileName
{
get
{
return myVars.iniFileName;
}
set
{
myVars.iniFileName = value;
}
}
#endregion

#region Section
///************************************************** **********************
/// <summary>
/// Section
/// </summary>
/// <returns>
/// The property Section returns a data type of string.
/// </returns>
/// <revisionhistory>
/// 3/29/2006 12:00:00 AM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public string Section
{
get
{
return myVars.section;
}
set
{
myVars.section = value;
}
}
#endregion

#endregion

#region Public Methods

/************************************************** *********************/
/* METHODS
*/
/************************************************** *********************/

#region GetString

///************************************************** **********************
/// <summary>
/// GetString
/// </summary>
/// <returns>
/// The method GetString returns a data type of string.
/// </returns>
/// <param name="keyName">keyName a variable of type string</param>
/// <revisionhistory>
/// 3/29/2006 2:51:54 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public string GetString(string keyName)
{

string localSection;
string defaultVal = "";

localSection = myVars.section;

if (localSection.Length == 0)
{
localSection = "Default";
}

return GetString(localSection, keyName, defaultVal);

}
#endregion

#region GetString
///************************************************** **********************
/// <summary>
/// GetString
/// </summary>
/// <returns>
/// The method GetString returns a data type of string.
/// </returns>
/// <param name="keyName">keyName a variable of type string</param>
/// <param name="defaultVal">defaultVal a variable of type
string</param>
/// <revisionhistory>
/// 3/29/2006 2:51:50 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public string GetString(string keyName, string defaultVal)
{
string localSection;

localSection = myVars.section;

if (localSection.Length == 0)
{
localSection = "Default";
}

return GetString(localSection, keyName, defaultVal);

}
#endregion

#region GetString
///************************************************** **********************
/// <summary>
/// GetString
/// </summary>
/// <returns>
/// The method GetString returns a data type of string.
/// </returns>
/// <param name="section">section a variable of type string</param>
/// <param name="keyName">keyName a variable of type string</param>
/// <param name="defaultVal">defaultVal a variable of type
string</param>
/// <revisionhistory>
/// 3/29/2006 2:51:46 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public string GetString(string section, string keyName, string
defaultVal)
{
int apiReturn;
StringBuilder apiBuffer = new StringBuilder(256);

try
{
apiReturn = GetPrivateProfileString(section, keyName,
defaultVal, apiBuffer, apiBuffer.Capacity, myVars.iniFileName);
if (apiBuffer == null)
return "";
else
return apiBuffer.ToString();
}
catch (Exception e)
{
Debug.WriteLine("GetSettingStr - Error: " + e.ToString());
return "";
}
}
#endregion

#region GetInt
///************************************************** **********************
/// <summary>
/// GetInt
/// </summary>
/// <returns>
/// The method GetInt returns a data type of int.
/// </returns>
/// <param name="keyname">keyname a variable of type string</param>
/// <revisionhistory>
/// 3/29/2006 3:16:00 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public int GetInt(string keyname)
{
return Convert.ToInt32(this.GetString(keyname));
}
#endregion

#region GetInt
///************************************************** **********************
/// <summary>
/// GetInt
/// </summary>
/// <returns>
/// The method GetInt returns a data type of int.
/// </returns>
/// <param name="keyname">keyname a variable of type string</param>
/// <param name="defaultVal">defaultVal a variable of type int</param>
/// <revisionhistory>
/// 3/29/2006 3:17:07 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public int GetInt(string keyname, int defaultVal)
{
return Convert.ToInt32(GetString(keyname, defaultVal.ToString()));
}
#endregion

#region GetInt
///************************************************** **********************
/// <summary>
/// GetInt
/// </summary>
/// <returns>
/// The method GetInt returns a data type of int.
/// </returns>
/// <param name="section">section a variable of type string</param>
/// <param name="keyname">keyname a variable of type string</param>
/// <param name="defaultVal">defaultVal a variable of type int</param>
/// <revisionhistory>
/// 3/29/2006 3:18:45 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public int GetInt(string section, string keyname, int defaultVal)
{
return Convert.ToInt32(GetString(section, keyname,
defaultVal.ToString()));
}
#endregion

#region SaveString
///************************************************** **********************
/// <summary>
/// SaveString
/// </summary>
/// <returns>
/// The method SaveString returns a data type of void.
/// </returns>
/// <param name="keyName">keyName a variable of type string</param>
/// <param name="setting">setting a variable of type string</param>
/// <revisionhistory>
/// 3/29/2006 2:51:43 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public void SaveString(string keyName, string setting)
{

string localSection;

localSection = myVars.section;

if (localSection.Length == 0)
{
localSection = "Default";
}

SaveString(localSection, keyName, setting);

}
#endregion

#region SaveString
///************************************************** **********************
/// <summary>
/// SaveString
/// </summary>
/// <returns>
/// The method SaveString returns a data type of void.
/// </returns>
/// <param name="section">section a variable of type string</param>
/// <param name="keyName">keyName a variable of type string</param>
/// <param name="setting">setting a variable of type string</param>
/// <revisionhistory>
/// 3/29/2006 2:51:37 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public void SaveString(string section, string keyName, string setting)
{
int apiReturn;

try
{
apiReturn = WritePrivateProfileString(section, keyName, setting,
myVars.iniFileName);
}
catch (Exception e)
{
Debug.WriteLine("SaveSettingStr - Error: " + e.ToString());
}
}
#endregion

#region SaveInt
///************************************************** **********************
/// <summary>
/// SaveInt
/// </summary>
/// <returns>
/// The method SaveInt returns a data type of void.
/// </returns>
/// <param name="keyname">keyname a variable of type string</param>
/// <param name="setting">setting a variable of type int</param>
/// <revisionhistory>
/// 3/29/2006 3:21:58 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public void SaveInt(string keyname, int setting)
{
SaveString(keyname, setting.ToString());
}
#endregion

#region SaveInt
///************************************************** **********************
/// <summary>
/// SaveInt
/// </summary>
/// <returns>
/// The method SaveInt returns a data type of void.
/// </returns>
/// <param name="section">section a variable of type string</param>
/// <param name="keyname">keyname a variable of type string</param>
/// <param name="setting">setting a variable of type int</param>
/// <revisionhistory>
/// 3/29/2006 3:21:55 PM - Robert C. Cain - Created.
/// </revisionhistory>
///************************************************** **********************
public void SaveInt(string section, string keyname, int setting)
{
SaveString(section, keyname, setting.ToString());
}
#endregion

#endregion

#region Constructors

/************************************************** *********************/
/* CONSTRUCTORS
*/
/************************************************** *********************/

public Ini()
{
}

public Ini(string appName)
{
myVars.iniFileName = appName.Trim() + ".ini";
}

public Ini(string appName, string iniFile)
{
myVars.iniFileName = iniFile.Trim();
}

#endregion

}
}

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Apr 24 '06 #5
Why re-invent the wheel? Have you tried the .NET "Nini" library? Open
source and supports all kinds of stuff.

Apr 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Wilfred Johnson | last post by:
I thought that I'd try php / mysql on windows 2000. I am running php 5.0.0 and it works fine, except when I try to use mysql functions. I get: Fatal error: Call to undefined function...
1
by: webhead74 | last post by:
Hi, I've worked on this for a couple of days & can't figure out what I'm doing wrong. I'm trying to connect to a remote AS400 database using odbc & php. Here's my environment: Server: RedHat...
4
by: Mark Hayworth | last post by:
Randy: I too am having the problem that this other guy/girl had. I put in the proper API declarations and arguments for GetPrivateProfileString yet it doesn't return the correct stuff. It always...
11
by: C. Sengstock | last post by:
Hi, i want to save the keywords of an ini file in a struct, together with a fpos_t. I think i´m right with the concept, but the access through fsetpos() doesn´t work. The position is always wrong...
7
by: Jack Lindamood | last post by:
PHP 5.1, Windows Server 2003, IIS 6, isapi I changed a line in the php.ini file to extension_dir = "c:\php\ext" but when I load my phpinfo page it shows extension_dir C:\php5 It's like my...
16
by: Steel | last post by:
Hi at all I installed locally the server APACHE 2 and PHP 5 I configured PHP as CGI with the folloring code into the apache configuration file ScriptAlias /php/ "c:/php/" AddType...
6
by: dylan.boudreau | last post by:
My configuration is Apache/2.0.59 (Unix) mod_ssl/2.0.59 OpenSSL/0.9.7d PHP/5.0.4 on Solaris 10. phpinfo() lists the location of php.ini as /usr/local/apache/conf and my php.ini file is there but...
6
by: howa | last post by:
ini_set( "short_open_tag", 1 ); phpinfo(); still showing 'off'
2
by: Colorado Trout | last post by:
From a command prompt, the calls to mysql work, but from a browser they do not.. Fatal error: Call to undefined function mysql_pconnect() in D:\work\php\chorelist.php on line 139 is the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.