Search code examples
c#windows-services.net-5akka.nettopshelf

File path passed to File.ReadAllBytes method are overridden by prepending executing path


File path passed to File.ReadAllBytes methodsare overridden by prepending executing path

This is console application running as a service using TopShelf in debug.

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

StackTrace

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, FileShare share, 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)

For debug purpose I added line - refer snip for it's value

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

enter image description here

It works for a simple .net5 console app with Topshelf. So I suspect Akka.net causing this.


Solution

  • Some non standard characters (not visible) are encoded before you "d:" in your string and that's why you have that behavior. If you rewrite your path manually (with the quotes) it will work.

    enter image description here

    In this example "filePathA" is copy-paste of your path and "filePathB" is the one I rewrote.

    enter image description here

    filePathA = 51 bytes

    filePathB = 45 bytes

    enter image description here