Description
Hello,
Description
I'm using .Net 8.0 and received an exception when using IO operations with long paths.
Example 1
I was trying to read a file form a source with a long path. I was not using a relative path.
A part of this path is using slash "/" characters instead of backslash "\" what causes an exception at File.ReadAllText().
var sourceFilePath = "Configuration/MyConfiguration.json"; // here I used a slash
if (!Path.IsPathRooted(sourceFilePath))
{
sourceFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), sourceFilePath);
// result: sourceFilePath = @"\\?\D:\Git\SubFolderA\SubFolderB\SubFolderC\SubFolderD\SubFolderF\MyVeryVeryVeryLongSolutionName\AnotherVeryLongSubfolder\MyVeryVeryVeryLongProjectName\bin\x86\Debug\net8.0-windows\Configuration/MyConfiguration.json"
// Assembly.GetExecutingAssembly().Location returns a path with "\\?\" prefix
}
var content = File.ReadAllText(sourceFilePath); // this throws an exception
Exception
System.IO.IOException: The syntax for the file name, directory name or data carrier designation is incorrect
When I ensure that the path only uses back slashes "\" or when I remove the prefix "\\?\" then my example is running successfully.
Example 2
But third part libraries might also use slashes instead of back slashes in their code. For example Grpc.Core.dll:
Exception
Exit code is -532462766 (Unhandled exception. System.IO.FileNotFoundException: Error loading native library. Not found in any of the possible locations: \\?\D:\Git\VeryLongPath\bin\x86\Debug\net8.0-windows\grpc_csharp_ext.x86.dll,\\?\D:\Git\VeryLongPath\bin\x86\Debug\net8.0-windows\runtimes/win-x86/native\grpc_csharp_ext.x86.dll,\\?\D:\Git\VeryLongPath\bin\x86\Debug\net8.0-windows../..\runtimes/win-x86/native\grpc_csharp_ext.x86.dll
at Grpc.Core.Internal.UnmanagedLibrary.FirstValidLibraryPath(String[] libraryPathAlternatives)
at Grpc.Core.Internal.UnmanagedLibrary..ctor(String[] libraryPathAlternatives)
at Grpc.Core.Internal.NativeExtension.LoadNativeMethodsUsingExplicitLoad()
at Grpc.Core.Internal.NativeExtension.LoadNativeMethods()
at Grpc.Core.Internal.NativeExtension..ctor()
at Grpc.Core.Internal.NativeExtension.Get()
at Grpc.Core.Internal.NativeMethods.Get()
at Grpc.Core.GrpcEnvironment.GrpcNativeInit()
at Grpc.Core.GrpcEnvironment..ctor()
at Grpc.Core.GrpcEnvironment.AddRef()
at Grpc.Core.Server..ctor(IEnumerable`1 options)
at Grpc.Core.Server..ctor()
Further technical details
.NET SDK:
Version: 8.0.403
Commit: c64aa40a71
Workload version: 8.0.400-manifests.e99c892e
MSBuild version: 17.11.9+a69bbaaf5
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.403\
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
[wasm-tools-net7]
Installation Source: VS 17.11.35327.3
Manifest Version: 8.0.10/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.net7\8.0.10\WorkloadManifest.json
Install Type: FileBased
[maccatalyst]
Installation Source: VS 17.11.35327.3
Manifest Version: 18.0.8303/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\18.0.8303\WorkloadManifest.json
Install Type: FileBased
[ios]
Installation Source: VS 17.11.35327.3
Manifest Version: 18.0.8303/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\18.0.8303\WorkloadManifest.json
Install Type: FileBased
[wasm-tools]
Installation Source: VS 17.11.35327.3
Manifest Version: 8.0.10/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.10\WorkloadManifest.json
Install Type: FileBased
[android]
Installation Source: VS 17.11.35327.3
Manifest Version: 34.0.113/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.113\WorkloadManifest.json
Install Type: FileBased
[maui-windows]
Installation Source: VS 17.11.35327.3
Manifest Version: 8.0.82/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.82\WorkloadManifest.json
Install Type: FileBased
Host:
Version: 8.0.10
Architecture: x64
Commit: 81cabf2857
.NET SDKs installed:
2.1.818 [C:\Program Files\dotnet\sdk]
3.1.426 [C:\Program Files\dotnet\sdk]
6.0.425 [C:\Program Files\dotnet\sdk]
8.0.403 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found