|
| 1 | +--- |
| 2 | +title: "Breaking change - Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory" |
| 3 | +description: "Learn about the breaking change in .NET 10 Preview 5 where specifying DllImportSearchPath.AssemblyDirectory as the only search flag restricts the search to the assembly directory." |
| 4 | +ms.date: 5/9/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/dotnet/docs/issues/45911 |
| 7 | +--- |
| 8 | + |
| 9 | +# Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory |
| 10 | + |
| 11 | +Starting in .NET 10, if you specify <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> as the only search flag, the runtime searches exclusively in the assembly directory. This change affects the behavior of P/Invokes and the <xref:System.Runtime.InteropServices.NativeLibrary> class. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 Preview 5 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +When <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> was specified as the only search flag, the runtime searched the assembly directory first. If the library was not found, it fell back to the operating system's default library search behavior. |
| 20 | + |
| 21 | +Example: |
| 22 | + |
| 23 | +```csharp |
| 24 | +[DllImport("example.dll", DllImportSearchPath = DllImportSearchPath.AssemblyDirectory)] |
| 25 | +public static extern void ExampleMethod(); |
| 26 | +``` |
| 27 | + |
| 28 | +In this case, the runtime would search the assembly directory and then fall back to the OS search paths. |
| 29 | + |
| 30 | +## New behavior |
| 31 | + |
| 32 | +When <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> is specified as the only search flag, the runtime searches only in the assembly directory. It does not fall back to the operating system's default library search behavior. |
| 33 | + |
| 34 | +The previous code example would now only search the assembly directory for *example.dll*. If the library is not found there, a <xref:System.DllNotFoundException> will be thrown. |
| 35 | + |
| 36 | +## Type of breaking change |
| 37 | + |
| 38 | +This is a [behavioral change](../../categories.md#behavioral-change). |
| 39 | + |
| 40 | +## Reason for change |
| 41 | + |
| 42 | +The fallback behavior when specifying <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> caused confusion and was inconsistent with the design of search flags. This change ensures clarity and consistency in behavior. |
| 43 | + |
| 44 | +## Recommended action |
| 45 | + |
| 46 | +If fallback behavior is required, avoid specifying an explicit <xref:System.Runtime.InteropServices.DllImportSearchPath>. By default, when no flags are specified, the runtime searches the assembly directory and then falls back to the operating system's default library search behavior. |
| 47 | + |
| 48 | +Example: |
| 49 | + |
| 50 | +```csharp |
| 51 | +[DllImport("example.dll")] |
| 52 | +public static extern void ExampleMethod(); |
| 53 | +``` |
| 54 | + |
| 55 | +## Affected APIs |
| 56 | + |
| 57 | +- P/Invokes using <xref:System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute> |
| 58 | +- <xref:System.Runtime.InteropServices.NativeLibrary.Load*?displayProperty=fullName> |
| 59 | +- <xref:System.Runtime.InteropServices.NativeLibrary.TryLoad*?displayProperty=fullName> |
0 commit comments