Click to See Complete Forum and Search --> : INI Files


Rugazi
May 7th, 2003, 04:47 PM
Hello can some one tell me how to read and write to ini files thanks if you can help :mad:

KingTermite
May 7th, 2003, 05:17 PM
There is a WinAPI call, something like GetPrivateProfileString() that reads values from INI files.

You probably have to tap into the win32 api to use it, but its pretty straightforward. You should consider using the registry though (variation of the same function)...INI files are pretty much obsolete.

pareshgh
May 7th, 2003, 05:55 PM
You can use StreamReader class to read that INI file as text file. There's a sample at
StreamReader (http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q304430&)

pareshgh
May 7th, 2003, 05:57 PM
I would personely suggest using XML files instead of INI files.

-Paresh

pareshgh
May 7th, 2003, 05:59 PM
or as suggested,
You will have to use the same win32 API functions that you used before
to read INI files. You will have to use the P/Invoke layer to make the
calls.


-Paresh

pareshgh
May 7th, 2003, 06:03 PM
and FYI
plz take a closer look at P/Invoke on MSDN - Consuming Unmanaged DLL functions (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconconsumingunmanageddllfunctions.asp)

Paresh

Rugazi
May 8th, 2003, 08:09 AM
Thanks for all you help :cool:

pareshgh
May 9th, 2003, 04:25 PM
Check Out this sample, you can have this as a dll,

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

// Keep this in your library namespace
// Library will contain utilities and eXtra other Libraries
namespace Library.Utilities.IniReadWrite
{

public class IniReadWrite
{
[DllImport("kernel32.dll")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string StrSection, string strkey, string StrDef, StringBuilder retVal, int nSize, string strIniPath);

public string strIniFullPath; // provide the full path name of the Ini File

public long WriteInIniFile(string strSection, string strWhatKey, string strTheValue)
{
return WritePrivateProfileString(strSection, strWhatKey , strTheValue, strIniFullPath); // make sure you make the property
}

public string ReadFromIniFile(string strSection, string strKey)
{
int n = 255;
StringBuilder strSB = new StringBuilder(n);
int i = GetPrivateProfileString(strSection, strKey, "", strSB, n, strIniFullPath);
return strSB.ToString();
}

} // end IniReadWrite
} // end Library.Utilities.IniReadWrite


Hope this will help you,
Paresh

clifgriffin
May 13th, 2003, 07:26 AM
I recommend the registry to.

The last time I did this I considered using INIs, but registry editing turned out to be SO much easier.

Clif

pareshgh
May 13th, 2003, 12:16 PM
well, Registry is typically a system database where you save the settings somestuff like that. !