共用方式為


單一檔案應用程式不再尋找執行檔目錄中的原生庫

先前,在 單一檔案 .NET 應用程式中,單一檔案可執行文件的目錄已在啟動期間新增至 NATIVE_DLL_SEARCH_DIRECTORIES 屬性。 因此,當載入非受控程式庫時,.NET 一律會搜尋應用程式目錄。 在具有 NativeAOT 的非 Windows 上, rpath 預設會設定為應用程式目錄,因此它一律會在應用程式目錄中尋找原生連結庫。

應用程式目錄在單一檔案應用程式中已不再新增至 NATIVE_DLL_SEARCH_DIRECTORIES,而且 NativeAOT 中的 rpath 設定已被移除。 在這兩種情況下, DllImportSearchPath.AssemblyDirectory (包含在 P/Invokes 的預設行為中)表示應用程式目錄。 如果您指定該值或保留預設值,.NET 會在應用程式目錄中尋找。 如果您指定沒有該值的旗標,.NET 就不會再出現在應用程式目錄中。

推出的版本

.NET 10

先前的行為

先前,載入原生連結庫時,單一檔案應用程式一律會檢查應用程式目錄。 在非 Windows 作業系統上,NativeAOT 應用程式在載入原生程式庫時,一律會在應用程式目錄中尋找。

例如,下列 P/Invoke 會在應用程式目錄中尋找lib,如果找到,就從那裡載入它。

[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern void Method()

新行為

從 .NET 10 開始,單檔應用程式只有在原生程式庫載入的搜尋路徑指出包含組件目錄時,才會在應用程式目錄中進行搜尋。

// Look in System32 on Windows.
// Search the OS on non-Windows.
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern void Method()

// Look next to the single-file app because assembly directory
// means application directory for single-file apps.
[DllImport("lib")
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
static extern void Method()

// Look next to the single-file app (because assembly
// directory is searched by default), then default OS search.
[DllImport("lib")
static extern void Method()

破壞性變更的類型

這是 行為變更。

變更的原因

現有的行為(即使搜尋路徑排除它,一律會查看應用程式目錄),也會導致混淆。 它也與在一般 (非單一檔案、非 NativeAOT) .NET 應用程式中處理搜尋旗標的方式不一致。

如果需要 P/Invoke 或原生程式庫載入的應用程式/元件目錄,且該目錄先前未被指定,請指定 DllImportSearchPath.AssemblyDirectory

RPATH如果需要在 NativeAOT 中使用設定,請明確地將對應的連結器參數新增至您的專案。

受影響的 API

另請參閱