Hi,
I figured that out but I have more questions regarding using
StringBuilder. I am also using WritePrivateProfileString and whenever I
want to assign a value to the string builder before I do a
WritePrivateProfileString, have I no choice but to do a new
StringBuilder(value), every time I want to assign a new value to the
StringBuilder type variable. Will that affect the amount of memory
used?
for ex : I have my class
public class INIFileOp
{
/* private members */
private StringBuilder _strInDir = new StringBuilder(80);
...
/* properties */
public string InputDir
{
get { return _strInDir.toString();
set { _strInDir = new StringBuilder(value); } // Do I have to
do the new every time I set the value?
}
...
/* Methods */
public void WriteINIFile()
{
WritePrivateProfileString("Directories", "In", _strInDir,
_strINIFile);
...
...
}
public void ReadINIFile()
{
GetPrivateProfileString("Directories" , "In" ,
"(rg)data/xml",_strInDir,80, _strINIFile);
...
...
}
}
public class Test
{
INIFileOp objINIFileOp = new INIfileOp();
objINIFileOp.ReadINIFile();
objINIFileOp.InputDir = "xyz/xyz"; // Actually in my code values
will be read from the form fields and set to the properties of
INIFileOp class.
objINIFileOp.WriteINIFile();
}
Any comments on using StringBuilders and the way I am doing it?
Thanks,
Lalasa.
David Anton wrote:
> You need to initialize the StringBuilder with the appropriate initial
size.
> StringBuilder _strInDir = new StringBuilder(80);
>
> David Anton
> www.tangiblesoftwaresolutions.com
> Home of the Instant C# VB.NET to C# converter and the Instant VB C#
to
> VB.NET converter
>
> "L" wrote:
>
> > [DllImport("kernel32.dll")]
> > static extern uint GetPrivateProfileString(
> > string lpAppName,
> > string lpKeyName,
> > string lpDefault,
> > StringBuilder lpReturnedString,
> > uint nSize,
> > string lpFileName);
> >
> >
> > Interoping GetPrivateProfileString is giving me this Exception in
> > certain cases:
> >
> > StringBuilder buffer has been overflowed by unmanaged code. The
> > process may become unstable. Insufficient capacity allocated to
the
> > StringBuilder before marshaling it."
> >
> > public void ReadINIFile()
> > {
> > try
> > {
> > GetPrivateProfileString("Directories" , "In" , "(rg)data/xml",
> > _strInDir,80, _strINIFile);
> > GetPrivateProfileString("Directories" , "Out", "c:\\xmldata" ,
> > _strOutDir,80, _strINIFile);
> > GetPrivateProfileString("System" , "IPAddress",
"198.190.229.101" ,
> > _strIP,80, _strINIFile);
> > GetPrivateProfileString("System" , "UserID", "jw" ,
_strUser,80,
> > _strINIFile);
> > GetPrivateProfileString("System" , "Password", "jason" ,
> > _strPwd,80, _strINIFile);
> > GetPrivateProfileString("Time" , "BusyTimeStart", "14:30" ,
> > _strBusyTimeStart,80, _strINIFile);
> > GetPrivateProfileString("Time" , "BusyTimeEnd", "15:30" ,
> > _strBusyTimeEnd,80, _strINIFile);
> > GetPrivateProfileString("Time" , "BusyTimeIntervalInMin", "1" ,
> > _strBusyTimeIntervalInMin,80, _strINIFile);
> > GetPrivateProfileString("Time" , "RegTimeIntervalInMin", "5" ,
> > _strRegTimeIntervalInMin,80, _strINIFile);
> > GetPrivateProfileString("File" , "MainFrame to PC File
extension",
> > "false" , _strFile,80, _strINIFile);
> > }
> > catch (Exception ex)
> > {
> > throw ex;
> > }
> > }
> >
> > Does anybody have a solution to this?
> >
> > Thanks,
> > Lalasa.
> >
> >