Bastianon Massimo
Bastianon Massimo

Reputation: 1742

Environment.SpecialFolder.ApplicationData returns the wrong folder

I have a strange problem: my .NET 4.0 WPF application is saving data to the ApplicationData folder.

 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\myProgram\\";

99.9% of the cases are working great, but on some computers it returns the wrong folder - instead of returning the user folder it returns another folder:

C:\Users\<user>\AppData\Roaming\myProgram\  --correct
C:\Users\s\AppData\Roaming\myProgram\       --wrong

The wrong folder has no write/read permission so my program doesn't work.

It seems the program is running under a different user, but if I check the Task Manager the user is the logged one.

The problem seems to be occurring with domain users with few permissions.

Upvotes: 21

Views: 42443

Answers (1)

Alex
Alex

Reputation: 181

Do you also create a text file to write?

If so save a file such as:

String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

var filePath = Path.Combine(path, "filetowrite.log"); // Handles whether there is a `\` or not.

if (File.Exists(filePath))
{
     ......................
}

Note also before doing any file operations, one should check if directory exists.

Upvotes: 18

Related Questions