4

I am trying to calculate sha1 hash for some of the files from location %system%\drivers\ using C#. I know files are at the exact location but when i use

FILE.Exists("c:\\Windows\\System32\\Drivers\\1394ohci.sys") 

it always retuns false.

C:\Users\administrator>dir c:\Windows\System32\drivers\1394ohci.sys
 Volume in drive C has no label.
 Volume Serial Number is 5A4F-1E60

 Directory of c:\Windows\System32\drivers

11/21/2010  08:53 AM           229,888 1394ohci.sys
               1 File(s)        229,888 bytes
               0 Dir(s)  19,521,245,184 bytes free


C:\Users\administrator>fciv -sha1 c:\Windows\system32\drivers\1394ohci.sys
//
// File Checksum Integrity Verifier version 2.05.
//
c:\windows\system32\drivers\1394ohci.sys\*
        Error msg  : The system cannot find the path specified.
        Error code : 3

I even tried fciv.exe on the file and it also generate the same output. I tried running the command as administratror but it did not help.

I did lot of web search but nothing worked. Please help and let me know how to fix this issue.

Appreciate your help. Thank you,

user1136789
  • 71
  • 2
  • 6
  • 2
    Does the running user have access permissions to 1394ohci.sys? – Shai Jan 08 '12 at 06:34
  • 2
    Are you running a 64-bit version of Windows? If so, file system redirection is kicking in. This is a 32-bit application, but you're asking it to look in a 64-bit directory. It's getting automagically redirected to `SysWOW64`, which probably doesn't contain `1394ohci.sys`. – Cody Gray - on strike Jan 08 '12 at 06:42
  • First of all your comment on nmjohn's answer belongs as a question comment. Second, compile your application as a 32 bit application. – M.Babcock Jan 08 '12 at 07:14

4 Answers4

4

If I understand your problem correctly then you need to look at File System Redirector

The %windir%\System32 directory is reserved for 64-bit applications. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference using a file system redirector.

In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64. Access to %windir%\lastgood\system32 is redirected to %windir%\lastgood\SysWOW64. Access to %windir%\regedit.exe is redirected to %windir%\SysWOW64\regedit.exe.

Also there is small sample at the bottom of page if you can try that one

string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32");
if(Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
// For 32-bit processes on 64-bit systems, %windir%\system32 folder
// can only be accessed by specifying %windir%\sysnative folder.
system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative");
}
Surjit Samra
  • 4,614
  • 1
  • 26
  • 36
  • Why does it not tell us the path of the file that couldn't be found? Makes it very frustrating to debug. – Collin Sep 15 '21 at 18:09
1

As others have mentioned, this is the file system redirector at work. The workaround is to replace system32 with sysnative in the filepath.

This was driving me bonkers too, and it took too much work to find the simple workaround. I kept landing on pages with advanced scripting and complicated, obscure tangentially-related solutions. So I thought I'd share the "easy mode".

Brady
  • 11
  • 1
0

Run your program in Administrator Mode.

nmjohn
  • 1,432
  • 7
  • 16
  • hi Codey Gray,Yes I am using 64 bit version of windows and yes SysWOW64 doesnt contain 1394ohci.sys file. How do i fix it? How can i stop redirection from code? – user1136789 Jan 08 '12 at 07:00
-1

From http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx:

The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125