传递给 File.ReadAllBytes 方法的文件路径通过添加执行路径被覆盖

问题描述

传递给 File.ReadAllBytes 方法文件路径通过预先执行路径覆盖

这是在调试中使用 TopShelf 作为服务运行的控制台应用程序。

var bytes = File.ReadAllBytes(@"‪‪d:\ubuntu-20.04.2.0-desktop-amd64.iso.torrent");

System.IO.IOException: 'The filename,directory name,or volume label Syntax is incorrect. : 'C:\FR\src\Service\bin\Debug\net5.0\‪D:\ubuntu-20.04.2.0-desktop-amd64.iso.torrent

堆栈跟踪

at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode,FileShare share,FileOptions options)
at System.IO.FileStream..ctor(String path,FileMode mode,FileAccess access,Int32 bufferSize,FileOptions options)
at System.IO.File.ReadAllBytes(String path)
at FR.Engine.Actors.FileReaderActor.OnReadFileCommand(FileReadCommand command) in C:\FR\src\FR.Engine\Actors\FileReaderActor.cs:line 18
at FR.Engine.Actors.FileReaderActor.<.ctor>b__0_0(FileReadCommand command) in C:\FR\src\FR.Engine\Actors\FileReaderActor.cs:line 11
at Akka.Tools.MatchHandler.PartialHandlerArgumentsCapture`2.Handle(T value)
at Akka.Actor.ReceiveActor.ExecutePartialMessageHandler(Object message,PartialAction`1 partialAction)
at Akka.Actor.ReceiveActor.OnReceive(Object message)
at Akka.Actor.UntypedActor.Receive(Object message)
at Akka.Actor.ActorBase.AroundReceive(Receive receive,Object message)
at Akka.Actor.ActorCell.ReceiveMessage(Object message)
at Akka.Actor.ActorCell.Invoke(Envelope envelope)

出于调试目的,我添加了一行 - 参考 snip 的值

var fullpath = Path.GetFullPath(command.Path);

enter image description here

它适用于带有 Topshelf 的简单 .net5 控制台应用程序。所以我怀疑是 Akka.net 造成的。

解决方法

一些非标准字符(不可见)在您的字符串中的“d:”之前被编码,这就是您有这种行为的原因。如果您手动(使用引号)重写路径,它将起作用。

enter image description here

在这个例子中,“filePathA”是你的路径的复制粘贴,“filePathB”是我重写的。

enter image description here

filePathA = 51 字节

filePathB = 45 字节

enter image description here