June 14th, 2013, 07:04 AM
My.Computer.FileSystem.FileExists("C:\Windows\System32\explorer.exe") - wrong answer
I have encountered a false positive from My.Computer.FileSystem.FileExists(filename) where there are no spaces in filename.
I am coding my own routine to find the first occurrence of a file in the directories listed in the %PATH% environment variable. I thought there would be one available ... but it seems not.
I take each directory in the PATH, and first check that the directory exists. If the directory exists I add the name of the file to the directory-name to get a full-path-name and check for FileExists(full-path-name).
My.Computer.FileSystem.FileExists("C:\Windows\System32\explorer.exe") returns TRUE ... but it does not exist. It cannot be access permissions ... because, as per MS documentation, inadequate permissions may give a false negative, but not a false positive.
However C:\Windows\explorer.exe does exist. Perhaps it is finding this. I cannot test this because I cannot rename it ..."Access is denied". But I did test by copying explorer.exe to a user directory and in this case I can look one folder either side of where I have copied explorer.exe and it correctly returns FALSE ... so it seems to be something related particularly to the C:\Windows\System32 folder structure.
The file C:\Windows\explorer.exe exists.
The file C:\Windows\System32\explorer.exe does not exist.
[I have folder options set to show hidden files, folders, drives and display protected operating system files ... so I would see it if it was there.]
My.Computer.FileSystem.FileExists("C:\explorer.exe") returns FALSE ... this is correct.
My.Computer.FileSystem.FileExists("C:\Windows\explorer.exe") returns TRUE ... this is correct.
My.Computer.FileSystem.FileExists("C:\Windows\System32\explorer.exe") returns TRUE ... this is NOT correct.
Hmmm. My system has a 64 bit processor.
The file "C:\Windows\SysWOW64\explorer.exe does exist. Maybe FileExists() treats System32 as an alias for SysWOW64 finding explorer.exe and Process.Start does not.
This is frustrating because having "found" ... file="C:\Windows\System32\explorer.exe" ... if you Process.start(file,etc.) ... you get "Can't find file" message. And I am not surprised because it does not exist.
I am using Process.Start(startInfo) with startInfo.UseShellExecute = FALSE so I can set USERNAME etc. otherwise I cannot subsequently identify the Explorer process.
If you use startInfo.UseShellExecute = TRUE, then you do not need to find the file because the shell execute uses the PATH to correctly to find the explorer app. How?
But the process structure returned is then not for the explorer-open-on-the-folder but for the explorer-startup-process ... which you cannot subsequently switch to.
So either I can start explorer but have no way to get a handle on the process.
Or I supply startInfo with domain, username password etc. to allow the process to be subsequently picked out of the process list ... but I cannot reliably find the executable to run it.
Even System.IO.FileInfo incorrectly reports that the file "C:\Windows\System32\explorer.exe" exists.
Operating System: Windows 7 Enterprise SP1
My initial problem was simple. A button to start Explorer in a particular folder.
Easy enough ... but I don't want another Explorer for every button click.
If I already have an Explorer window started ... then switch to it.
Otherwise start one up and somehow keep tabs on the process that I start so I can switch to it later ... if it is still there.
But I am unable to start Explorer in such a way that I can subsequently identify it. Stumped.
June 17th, 2013, 09:35 AM
Re: My.Computer.FileSystem.FileExists("C:\Windows\System32\explorer.exe") - wrong ans
hmm... I have not used the My.Computer method for checking files. I generally use System.Io.File.Exists() though I'm not sure that would make a difference in this case. May be worth a try.
Always use [code][/code] tags when posting code.
Click Here to Expand Forum to Full Width