问题
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
回答1:
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";
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/8690251/file-exists-in-windows-explorer-and-notepad-but-is-not-accessable-in-my-program