File exists in Windows Explorer and notepad, but is not accessable in my program

て烟熏妆下的殇ゞ 提交于 2019-12-01 08:12:58

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";

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!