System.IO.File.Exists(@“C:\Windows\System32\SnippingTool.exe”) returns false

巧了我就是萌 提交于 2019-12-19 19:48:48

问题


So I am working on a simple .NET program for work and hit a bit of a weird roadblock I can't figure out. The application makes use of a configuration file which adds applications to a dropdown menu. However, before it's added to the menu, it does a system.io.file.exists to verify that the file exists, at least at the time the menu is created.

However, System.IO.File.Exists(@"C:\Windows\System32\SnippingTool.exe") returns false, but the executable is there. Likewise, stikynot.exe also returns false. Other files in the system32 directory return true(like notepad). I can filestat these applications, but my .net program just can't read them them and system.io.file.exists returns false. I have checked various permissions with no luck. It seems to be isolated to a few applications, but I am not sure why.


回答1:


That's a feature of 64 bit Windows.

In x64 you have two folders with system files: system32 and SysWOW64.

If you put anything in system32, it will be seen by all 64 bit applications. SysWOW64 is there for compatibility reasons - anything put there will be visible by 32 bit (sic!) apps.

So as far as .NET is concerned, the file does not exist. It works for notepad, because notepad.exe is present in both of those directories.

You can test this behaviour. Create a text file in one of the directories, but not the other and check for its existence. Then change your project configuration (right click on solution -> Configuration Manager) between x86 and x64 and enjoy the result.




回答2:


I have a crazy bug relate to this topic. Thank ya23.

In my case, just changing project Properties > Build > uncheck Prefer 32-bit



来源:https://stackoverflow.com/questions/9993733/system-io-file-existsc-windows-system32-snippingtool-exe-returns-false

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