Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.SpecialFolder.ApplicationData returns the wrong folder

Tags:

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.

like image 810
Bastianon Massimo Avatar asked Jun 05 '13 15:06

Bastianon Massimo


1 Answers

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.

like image 56
Alex Avatar answered Oct 20 '22 22:10

Alex