File.exsit查询system32下dll出错|windows64 文件重定向机制

现象:最近做的一个项目发现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个小时 记下来防止大家浪费时间

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值