文件存在于Windows资源管理器和记事本中,但是在我的程序中无法访问 [英] File exists in Windows Explorer and notepad, but is not accessable in my program
问题描述
为什么会出现以下问题?
Why does the following problem happen?
场景:
- 确保已安装IIS
- 使用管理员执行
"notepad %WINDIR%\System32\inetsrv\config\applicationHost.config"
帐户
- 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)));
实际结果:错误
预期结果:是
推荐答案
问题是,如果您在64位OS上运行32位应用程序,则.Net框架会自动将请求从%WINDIR%\System32
重定向到%WINDIR%\SysWOW64
.
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
.
如果将项目更改为64位目标,这将解决您的问题.
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";
这篇关于文件存在于Windows资源管理器和记事本中,但是在我的程序中无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!