Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

.net - System.IO.File.Exists(@"C:WindowsSystem32SnippingTool.exe") returns false

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:WindowsSystem32SnippingTool.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...