Franz B.
Franz B.

Reputation: 452

Environment.GetFolderPath(Environment.SpecialFolders.ApplicationData) returns C:

I have encountered a very strange behavior on the computer of one of my clients and I cannot find any clue as to why it happens: When the application calls Environment.GetFolderPath(Environment.SpecialFolders.ApplicationData) the return value will be C:.

This is of course wrong, his AppData directory is the usual C:\Users\.....\AppData\Roaming and also his variable %APPDATA% points to exactly that directory.

Can anybody shed light on why this could possibly happen?

EDIT: The code...

LogFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\ReportsAddin";
if (!Directory.Exists(LogFilePath) && Properties.Settings.Default.Logging == true)
{
    try
    {
        Directory.CreateDirectory(LogFilePath);
    }
    catch (Exception ex)
    {
        // ...
    }
}

The exception thrown then says that it cannot create a directory consisting of a blank string or blank spaces. Investigating with some output showed that the AppData folder returning from that call is C: when in fact it should be the user's real AppData folder.

Upvotes: 4

Views: 6631

Answers (1)

helb
helb

Reputation: 7793

The actual path for the folder identified by Environment.SpecialFolder.ApplicationData depends on the current user (who started the program).

Make sure the program runs under a user account for which the ApplicationData folder exists. If your program runs under e.g. a local system account you may want to use another directory.

Instead of Environment.SpecialFolder.ApplicationData you could use Environment.SpecialFolder.CommonProgramFiles or Environment.SpecialFolder.CommonProgramFilesX86.

Upvotes: 1

Related Questions