Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't always make 'System.IO.Directory.Exists' work in C#...

911 views
Skip to first unread message

Svend Dyhr Hansen

unread,
Oct 17, 2002, 3:21:38 PM10/17/02
to
I have a strange problem with C#...

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

der Hund

unread,
Oct 17, 2002, 4:20:30 PM10/17/02
to
Could it be that you're not escaping the "\" character? Using your example,
shouldn't you use:

@"C:\ALINC"

Just a guess..

Bret

"Svend Dyhr Hansen" <svend...@sundbakken.dk> wrote in message
news:ez#w8IhdCHA.1820@tkmsftngp12...

Jon Skeet

unread,
Oct 17, 2002, 4:50:34 PM10/17/02
to
Svend Dyhr Hansen <svend...@sundbakken.dk> wrote:
> I have a strange problem with C#...
>
> 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...

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

Svend Dyhr Hansen

unread,
Oct 17, 2002, 4:46:42 PM10/17/02
to
I've tried it - it makes no difference, I'm sorry....

Thanks anyway.

Svend
"der Hund" <nos...@me.com> wrote in message
news:Ov7E2phdCHA.2712@tkmsftngp10...

Svend Dyhr Hansen

unread,
Oct 17, 2002, 5:05:44 PM10/17/02
to
I've tried all that without success: the length is as expected. When
extracting a substring in order to create a new clean string to work with...
the problem remains the same...

Thanks for your time...
Svend

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.181932f7c...@dnews.peramon.com...

Chris R. Timmons

unread,
Oct 17, 2002, 5:09:44 PM10/17/02
to
Svend,

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/

Svend Dyhr Hansen

unread,
Oct 18, 2002, 3:25:58 AM10/18/02
to
Chris,

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...

0 new messages