I read a value (a directory path) from a .INI file via Win32 API into a
private string variable in a class.
Seems alright: the string variable is what I expect, for instance
"C:\ALINC".
However, when I use the System.IO.Directory.Exists method on the variable,
it returns false.
But the directory does indeed exist.
If I assign the same value to the variable from a Textbox.Text property the
method returns true.
Same, if I assign it in any other way, for instance from a constant, it
returns true.
Also if I type
Debug.Print System.IO.Directory.Exists("C:\ALINC")
it returns true
I think it might be the format of the string returned from the API call, but
I can't see whats wrong...
Here is how I read a .INI file:
[DllImport("kernel32.Dll", CharSet=CharSet.Auto)]
private static extern long GetPrivateProfileString(
System.String lpApplicationName,
System.String lpKeyName,
System.String lpDefault,
System.String lpReturnedString,
System.Int32 nSize,
System.String lpFileName);
public string getValue(string sectionName, string keyName, string
defaultValue)
{
const int maxlen = 255;
string sBuffer = "";
long retVal;
sBuffer = sBuffer.PadLeft(maxlen);
retVal = GetPrivateProfileString(sectionName, keyName, defaultValue,
sBuffer, maxlen, m_fileName);
return sBuffer;
}
Any help would be highly appreciated - a mail would indeed be nice as
well...
Best,
Svend Dyhr Hansen
s...@europaeiske.dk
@"C:\ALINC"
Just a guess..
Bret
"Svend Dyhr Hansen" <svend...@sundbakken.dk> wrote in message
news:ez#w8IhdCHA.1820@tkmsftngp12...
Have a look at the exact contents of the string, including its length.
Do this using Console.WriteLine rather than the debugger, as that has
some strange bugs in strings. Write out the unicode value of each
character in the string.
My guess is that you've got a 255 character string which just happens to
have a null character somewhere along it - you'll want to take the
substring of that up to but not including the null character, if so.
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Thanks anyway.
Svend
"der Hund" <nos...@me.com> wrote in message
news:Ov7E2phdCHA.2712@tkmsftngp10...
Thanks for your time...
Svend
"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.181932f7c...@dnews.peramon.com...
Three things to try:
1. Use a StringBuilder instead of a regular string for the buffer
in GetPrivateProfileString.
2. Change the CharSet attribute to either Ansi or Unicode.
3. Define an entry point for GetPrivateProfileString. If no
entry point is defined, GetPrivateProfileString will default to
using the ANSI character set version (GetPrivateProfileStringA).
Try changing the entry point to GetPrivateProfileStringW if you
are using a multi-byte character set.
This code works on my system (US English):
[DllImport("kernel32",
EntryPoint="GetPrivateProfileStringA",
CharSet=CharSet.Ansi)]
private static extern long GetPrivateProfileString(
System.String lpApplicationName,
System.String lpKeyName,
System.String lpDefault,
System.Text.StringBuilder lpReturnedString,
System.Int32 nSize,
System.String lpFileName);
public string getValue(string sectionName,
string keyName, string defaultValue)
{
const int maxlen = 255;
StringBuilder sBuffer = new StringBuilder(maxlen);
GetPrivateProfileString(sectionName, keyName, defaultValue,
sBuffer, maxlen, m_fileName);
return sBuffer.ToString();
}
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
You were absolutely right! That helped!
Thanx a lot everyone for helping me solving this problem...
Best,
Svend
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xns92AAA4A22CECBcr...@207.46.239.39...