15

I want my MVC3 web application to access %APPDATA% (e.g. C:\Users\MyUsername\AppData\Roaming on Windows 7) because I store configuration files there. Therefore I created an application pool in IIS with the identity of the user "MyUsername", created that user's profile by logging in with the account, and turned on the option "Load User Profile" (was true by default anyway). Impersonation is turned off.

Now I have the problem that %APPDATA% (in C#):

appdataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

resolves to c:\windows\system32\inetsrv instead of C:\Users\MyUsername\AppData\Roaming.

UPDATE: More exactly, the above C# code returns an empty string, so that Path.GetFullPath(Path.Combine(appdataDir, "MyAppName")) prepends the current path to my application name, resulting in c:\windows\system32\inetsrv\MyAppName.

I know I made this work before with the same web application on a Windows Server 2008 R2, and now I'm getting this problem with the same major version 7.5 of IIS on my Windows 7.
I used the same procedure as before: Created a new user, logged in as that user to create the profile and APPDATA directories, then added the application pool with this identity and finally added the web application to this pool.

Any ideas?

2
  • Is your application pool configured as Classic or Integrated mode?
    – Kev
    Feb 29, 2012 at 1:56
  • I have the same problem. What's especially weird is that the path for Environment.SpecialFolder.UserProfile works fine, and if I build up the path to the AppData folder from there, it works.
    – Jason
    Apr 20, 2012 at 20:58

5 Answers 5

23

Open your %WINDIR%\System32\inetsrv\config\applicationHost.config and look for <applicationPoolDefaults>. Under <processModel>, make sure you don't have setProfileEnvironment="false". If you do, set it to true.

5
  • The value is <processModel identityType="ApplicationPoolIdentity" />.
    – AndiDog
    Dec 10, 2012 at 21:19
  • Set it to <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
    – Amit Apple
    Dec 12, 2012 at 2:07
  • 2
    This answer is the one that solved the problem for me.
    – anon
    Jun 18, 2014 at 9:03
  • In my case, it was enough to add setProfileEnvironment="true" to the specific application pool. It was not necessary to change the setProfileEnvironment value in the default entry.
    – Heinzi
    Jan 16, 2018 at 15:23
  • 2
    Why is LoadUserProfile an option in IIS Manager, but SetProfileEnvironment is not?
    – Triynko
    Jan 10, 2019 at 20:00
5

Application Pools - Your application Pool - Advanced settings ...

Process Model - Load user Profile set True.

It Helps me.

Taken from https://blogs.msdn.microsoft.com/vijaysk/2009/03/08/iis-7-tip-3-you-can-now-load-the-user-profile-of-the-application-pool-identity/

3

I experienced the same problem recently. As mentioned by Amit, the problem is that the user profile isn't loaded. The setting is for all application pools, and is in the applicationHost.config (typically C:\Windows\System32\inetsrv\config\applicationHost.config). If you update the applicationPoolDefaults elements as follows, it will work;

<applicationPoolDefaults managedRuntimeVersion="v4.0">
  <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</applicationPoolDefaults>

We've tried this with IIS 7.5, and taken it through to production without problem.

You can automate this if you want;

appcmd set config -section:system.applicationHost/applicationPools /applicationPoolDefaults.processModel.setProfileEnvironment:"true" /commit:apphost

or if you prefer powershell

Set-WebConfigurationProperty "/system.applicationHost/applicationPools/applicationPoolDefaults/processModel" -PSPath IIS:\ -Name "setProfileEnvironment" -Value "true"

Hope this helps

0

I am experiencing the same problem. Have you by chance installed the Visual Studio 11 beta? I did recently, and I've noticed a couple of differences in how the 4.0 compatible .dlls for that work with our code. I'm still trying to track down the problem for certain, but I didn't have this problem before that.

Edit:

After comparing the decompiled sources from 4.0 and 4.5 for GetFolderPath (and related), there are differences. Whether they are the source of the problem...I'm not sure yet.

Edit 2: Here are the relevant changes. I'm working on trying both to see if I get different results. [code removed]

Edit 3:

I've now tried calling SHGetFolderPath directly, which is what the .NET Framework ends up doing, anyway. It returns E_ACCESSDENIED (-2147024891 / 0x80070005). I don't know what has changed where I'm getting that in some specific cases, but not in others.

Edit 4:

Since you're getting a empty string, you may want to switch your code to use SHGetFolderPath so you can get the HResult and at least know what exactly is happening.

void Main() {
    Console.WriteLine( GetFolderPath( Environment.SpecialFolder.ApplicationData ) );
}

[System.Runtime.InteropServices.DllImport("shell32.dll")]
static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, StringBuilder pszPath);

private string GetFolderPath( Environment.SpecialFolder folder ) {
    var path = new StringBuilder( 260 );
    var hresult = SHGetFolderPath( IntPtr.Zero, (int) folder, IntPtr.Zero, 0, path );
    Console.WriteLine( hresult.ToString( "X" ) );

    return ( (object) path ).ToString( );
}
2
  • 1
    No, I have Visual Studio 2010 installed, and no other betas. Which differences are there?
    – AndiDog
    Apr 23, 2012 at 12:51
  • I am also getting 0x80070005 (E_ACCESSDENIED). Will try to investigate more.
    – AndiDog
    May 8, 2012 at 14:50
0

The problem is with your IIS settings. The answer is here: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) returns String.Empty

4
  • I have the user profile loaded and get the same problem (I think this problem occurs on Windows 7 only).
    – AndiDog
    Jul 19, 2012 at 14:36
  • Based on what you described in your original question, you did create the profile folders, but you didn't take the step necessary to load the profile for IIS. If you did take that step then add it to your description. If you didn't then read the link I gave and follow the instructions.
    – grahamesd
    Jul 23, 2012 at 17:16
  • I said in my question that I had that option turned on, so this solution doesn't seem to work for me, sorry. Must have something to do with Windows 7. When I have time, I'll try on Windows 8 (not the server variant) and see what happens.
    – AndiDog
    Jul 23, 2012 at 20:02
  • I think you're right. I tried it with IIS on Win7 Pro and on Win7 Enterprise and GetFolderPath returns an empty string. Yet on Win2008 R2 it works fine. I also tried changing the app pool identity in Win7 to an account with higher priveliges and then got back "C:\Windows\system32\config\systemprofile\AppData\Roaming" which doesn't even exist. So my conclusion is the same as yours: it doesn't work on Win7. Good news is that it DOES work for IIS Express, so you can at least develop on your Win7 machine.
    – grahamesd
    Aug 1, 2012 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.