Absolute Reference to Application Data Directory

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

Guest

I have an app that stores information in the Application Data directory and
works fine when it's installed on the device. I just reference "\Application
Data\MySpecialDir" and I'm set. However, if the application is installed on
an SD card this reference fails.

How do I address the Application Data directory on the device itself,
absolutely rather than relatively the way "\Application Directory" does?

Thanks in advance for answering what is likely a very basic question.
 
Hi!

GetFolderPath is the right way.. as long as Application Data folder exists.
Some devices don't have it by default, so it needs to be created. Here comes
my problem..

I would really like to use this nice SpecialFolder enumeration to create
Application Data folder (which has different names in different languages, of
course), but Directory.Create doesn't cooperate (it requires a string).

Do you know how it's supposed to be done (is there any good way)?
Please help!

Adam
 
Thank you for your answer.
Unfortunately, it's not that simple..

The thing is, that doesn't work if Application Data doesn't exist.

The code you posted throws an InvalidOperationException (in
System.Environment.GetFolderPath()).

It's simple to reproduce - I call that code after renaming Application Data.

I am using .NET CF 2.0 (no SP, if that matters)
 
Try this:

const int CSIDL_APPDATA = 0x1a;

[DllImport("coredll",SetLastError=true)]
public static extern bool SHGetSpecialFolderPath(IntPtr hWnd, StringBuilder
lpszPath, int nFolder, bool fCreate);

StringBuilder folderPath = new StringBuilder(260);
bool result = SHGetSpecialFolderPath(IntPtr.Zero, folderPath ,
CSIDL_APPDATA, true);

The last parameter, true, will instruct SHGetSpecialFolderPath to create the
folder if it doesn't already exist.

The .NET Compact Framework uses this same API call through a PAL wrapper
method call System.PInvoke.PAL.Environment_GetSpecialFolder but it sets the
fCreate parameter as false.

--
Neil Cowburn
Principal Partner
OpenNETCF Consulting, LLC.

Managed Code in the Embedded World

http://www.opennetcf.com/
http://www.smartdeviceframework.com/
 
Back
Top