3

Why does the following problem happen?

Scenario:

  • Make sure that IIS is installed
  • Execute "notepad %WINDIR%\System32\inetsrv\config\applicationHost.config" using admin account

Actual Result: the file is successfully opened in notepad

  • Execute the following code in admin account's context:

    string filePath = @"%WINDIR%\System32\inetsrv\config\applicationHost.config";
    Console.WriteLine(File.Exists(Environment.ExpandEnvironmentVariables(filePath)));
    

Actual Result: False

Expected Result: True

Rob
  • 26,989
  • 16
  • 82
  • 98
Alen Pelin
  • 66
  • 6
  • 1
    Out of interest, what does Console.WriteLine(Environment.ExpandEnvironmentVariables(filePath)) give you? – dash Dec 31 '11 at 20:00
  • 4
    Check the permissions - if the account under which the application is running does not have permissions to this path, the file will not be found. – Oded Dec 31 '11 at 20:00
  • I discounted permissions as the OP mentions he was able to access the file using the admin account, but not through code running as the admin account. Always worth double checking though. – dash Dec 31 '11 at 20:05
  • Double-check that %WINDIR% exists in the environment variables. Like @dash mentioned, I usually use Environment.SpecialFolder.System as well. I've seen environment variables get deleted on systems and don't rely on them. – CtrlDot Dec 31 '11 at 20:11
  • As @Oded said, check the file permissions. If you're running your code sample from Visual Studio and you're on Vista or 7, then make sure Visual Studio is running as Administrator. – M.Babcock Dec 31 '11 at 20:14
  • Windows UAC problem? Disable UAC and execute your code. – Gabriel Santos Dec 31 '11 at 20:19
  • @AlenPelin This is a dumb question, but: all this is happening on the same machine, right? You're not running the code on a server and Notepad on a client machine, are you? – Ann L. Dec 31 '11 at 20:22
  • Permissions are default: full access for local administrators, system and trustedinstaller. Double checked it and reproduced the problem on two independent Windows 7 x64 sp1 machines with disabled UAC. @AnnL. The question is probably dumb, I'll not argue with you. But, of course, the notepad and the app was tested on the same machine. – Alen Pelin Dec 31 '11 at 20:44
  • Can you open the file with another app besides notepad? Notepad doesn't use file locks - so this maybe the issue. – tsells Dec 31 '11 at 20:50
  • 1
    @AlenPelin (In case it wasn't clear, I meant that *my* question was dumb, not yours! No offense was meant! Good luck with finding an answer!) – Ann L. Dec 31 '11 at 21:14
  • 2
    Almost certainly file redirector at play in x86 process. – David Heffernan Dec 31 '11 at 22:11
  • @AnnL. thank you, this is much clearer :) – Alen Pelin Dec 31 '11 at 23:07
  • @DavidHeffernan you are right, changing platform to x64 solved the problem. – Alen Pelin Dec 31 '11 at 23:09

3 Answers3

3

The problem is if you are running a 32-bit application on a 64-bit OS, the .Net framework automatically redirects the request from %WINDIR%\System32 to %WINDIR%\SysWOW64.

If you change your project to target 64-bit, this will solve your problem.

You can also resolve the problem by changing System32 to sysnative, but only if you leave the application as a 32-bit app:

string filePath = @"%WINDIR%\sysnative\inetsrv\config\applicationHost.config";
competent_tech
  • 44,465
  • 11
  • 90
  • 113
2

This might be due to file system redirection. AFAIK t happens either for 32/64 bit mismatch or in case of low-privilege (UAC) processes.

I know of now way of disabling that behavior using managed APIs. You need to use http://msdn.microsoft.com/en-us/library/windows/desktop/aa365743(v=vs.85).aspx and/or be a high privilege process.

If you change your project to target 64-bit, this is likely to solve your problem.

usr
  • 168,620
  • 35
  • 240
  • 369
0

I can't reproduce your result. When I run this from an administrator command line prompt, I get exists = True.

string s = @"%WINDIR%/System32\inetsrv\config\applicationHost.config";
bool exists = File.Exists(Environment.ExpandEnvironmentVariables(s));
Console.WriteLine("exists = {0}", exists);

I'm running Windows Server 2008, 64-bit. .NET 4.0.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351