Reading INI File

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to read INI File from inside the C#
I Created Class:
public class clsIniFile
{
[DllImport("kernel32.dll", SetLastError=true)]
private static extern int WritePrivateProfileStringA(
string lpApplicationName,
string lpKeyName,
string lpString,
string lpFileName);

[DllImport("kernel32.dll", SetLastError=true)]
private static extern int GetPrivateProfileStringA(
string lpApplicationName,
string lpKeyName,
string lpDefault,
string lpReturnedString,
int nSize,
string lpFileName);

public string filename
{
get
{
return filename;
}
set
{
filename = value;
}
}


private const int Max_SectionBuffer = 4096;
private const int Max_EntryBuffer = 255;

public clsIniFile()
{

}

public bool FileExist(string filename)
{
return File.Exists(filename);
}

public void writeEntry(string Section, string EntryName, string Value,
string filename)
{
WritePrivateProfileStringA(Section, EntryName, Value, filename);
}

public string PrivGetString(string Section, string EntryName, string
filename)
{
int nRetVal, nBuffersize;
string sTemp = "";
sTemp = sTemp.PadLeft(255, ' ');
nBuffersize = sTemp.Length;

nRetVal = GetPrivateProfileStringA(Section, EntryName, "", sTemp,
nBuffersize, filename);
if (nRetVal != 0)
return sTemp.Substring(1 , nRetVal);
else
return "";
}
}

End this class works fine when I'm writing to INI File but when I'm trying
to read it always return empty string in the sTemp. What's wrong. Thanks.
 
The parameter "lpReturnedString" in GetPrivateProfileStringA should be
declared as System.Text.StringBuilder since .net standard strings are
immutable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top