现象:最近做的一个项目发现File.Exist查找system32下的dll时 明明dll不存在 但是返回依然是存在 尝试了CopyTo 到D盘 发现的确能拷贝出来 后面看了下syswow64 发现里面有同名文件 而且大小跟D盘拷贝出来的一致。MMP...
查了下发现64位windows操作系统存在文件系统重定向机制 也就是即便system32下不存在某文件但是syswow64下存在同名文件的话File.exist依然判断system32下存在该文件...
然后为了解决这个问题需要暂时关闭文件系统重定向
https://www.cnblogs.com/iamlucky/p/5998086.html
根据这个大佬的文章 可以解决问题
代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace FindFileTest
{
class Program
{
static void Main(string[] args)
{
string DLL1 = @"C:\Windows\System32\msvcp140.dll";
string DLL2 = @"C:\Windows\System32\vcruntime140.dll";
IntPtr oldWOW64State = new IntPtr();
Wow64DisableWow64FsRedirection(ref oldWOW64State); // 关闭64位(文件系统)的操作转向
Console.WriteLine( File.Exists(DLL1));
Console.WriteLine(File.Exists(DLL2));
Wow64RevertWow64FsRedirection(oldWOW64State);
Console.WriteLine(File.Exists(DLL1));
Console.WriteLine(File.Exists(DLL2));
Console.ReadKey();
}
// 关闭64位(文件系统)的操作转向
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
// 开启64位(文件系统)的操作转向
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
}
}
从搞清楚到解决花了大概3个小时 记下来防止大家浪费时间