[英]Check File.Exists in System32 with Sysnative and %windir% from 32-bit app C#
我已經在C#中創建了一個32位應用程序。 因為它是32位的,所以它無法訪問64位的位置,例如C:\\ Windows \\ System32。 要訪問64位位置,我知道我需要改為使用C:\\ Windows \\ Sysnative。
我想檢查System32中是否存在文件。 我在這里放置了一個文件,並且可以在文件瀏覽器中查看它,因此我知道它存在。
但是,當我使用以下代碼時,File.Exists方法始終返回false。 系統化似乎根本沒有重定向。 如何在%windir%和Sysnative中同時使用File.Exists從32位應用程序在System32中查找文件?
string fileName = "someFile.dll";
string path = Environment.ExpandEnvironmentVariables(@"%windir%\Sysnative\" + fileName);
//path should be 'C:\Windows\System32\'+fileName;
bool fileExists = File.Exists(path); //Always returns false
我做了一個快速測試,結果發現,如果您使用的是x64,則fileExists
返回False
而x86和Any CPU則返回True
。 至於原因,我不知道,但是由於您打算在32位應用程序上使用它,因此只需使用x86。 確保選擇x86,以便任何CPU都不會錯誤地選擇x64。
eryksun:
原因很簡單,“ SysNative”不是真實目錄。 它被WOW64仿真系統重定向到真實的“ System32”目錄。 僅通過WOW64在64位Windows上運行的32位應用程序可以使用此虛擬目錄。 在32位Windows上運行的32位應用程序不能使用“ SysNative”,因此即使在32位應用程序中也必須嘗試使用“ System32”。
碼
class Program
{
static void Main(string[] args)
{
string fileName = "aeevts.dll";
string path = Environment.ExpandEnvironmentVariables(@"%windir%\Sysnative\" + fileName);
//path should be 'C:\Windows\System32\'+fileName;
Console.WriteLine(path);
bool fileExists = File.Exists(path); //Always returns false
Console.WriteLine(fileExists);
Console.Read();
}
}
性質
OP
在任何CPU上運行,只有在Platform Target上選中“ prefer 32-bit”(請參見上面的屏幕截圖)時,Sysnative才能工作並且File.Exists返回true
x86 /任何CPU輸出
x64輸出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.