Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) returns String.Empty

i have an asp.net mvc3 application. Now i want to save userdata in

C:\Users{AppPoolUserAccount}\AppData\Roaming\MyProgramm...

On first call of Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) i only get "" (String.Empty). On second call Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) returns correct path...

Note: The routine is in a Login-Context. I want to save username and sessionID in a xml-file to prevent that two users are logged in simultaneously via one user-account.

Why?

like image 821
Matthias Meyer Avatar asked Feb 22 '23 08:02

Matthias Meyer


2 Answers

Environment.GetFolderPath returns empty strings for most of the SpecialFolder enum values because the user profile for the user you are using to run the app pool is not loaded.

You need to configure the app pool to load the user profile either by going into IIS Manager > Application Pools > YourAppPool > Advanced Settings > Load User Profile, and setting the value to "true" or by opening up a command prompt and running

appcmd set apppool "MyAppPool" -processModel.loadUserProfile:true

(usually you'll run this in C:\Windows\SysWOW64\inetsrv).

Here are a couple of links with more data:

  • loadUserProfile and IIS7 (This one says that loading the profile is the default, which it isn't, but it is correct otherwise. Maybe they changed the default between 7 and 7.5?)
  • Process Model Settings for an Application Pool
like image 140
grahamesd Avatar answered Apr 06 '23 18:04

grahamesd


Environment.GetFolderPathreturns an empty string by design if the folder does not exist.

From MSDN:

Environment.GetFolderPath Method (Environment.SpecialFolder)

Return Value Type: System.String

The path to the specified system special folder, if that folder physically exists on your computer; otherwise, an empty string ("").

A folder will not physically exist if the operating system did not create it, the existing folder was deleted, or the folder is a virtual directory, such as My Computer, which does not correspond to a physical path.

like image 34
Reg Edit Avatar answered Apr 06 '23 18:04

Reg Edit