21

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.

Bridge
  • 29,818
  • 9
  • 60
  • 82
Bastianon Massimo
  • 1,710
  • 1
  • 16
  • 23

1 Answers1

17

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Alex
  • 171
  • 1
  • 3
  • 10
    Always use Path.Combine, never "+" as you do not know if the path ends with a backslahs (or uses backslashes at all). – nivs1978 Oct 15 '15 at 11:12