1

I'm getting to load my file from disk but I'm getting an error: System.IO.IOException.

FileStream attachment;
using (var stream = File.Open(@"c:\testFolder\test.txt", FileMode.Open))
{
 attachment = stream;
}

Error from logs:

Exception thrown: 'System.IO.IOException' in System.IO.FileSystem.dll Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.ni.dll 2017-04-20 07:19:08 [Error] Invalid argument

The file exists.

What I'm doing wrong?

I'm using Asp.Net Core 1.1.

Set
  • 47,577
  • 22
  • 132
  • 150
mskuratowski
  • 4,014
  • 15
  • 58
  • 109
  • 2
    Usually there is an additional information when an exception is thrown. Can you share it too ? ( call `.getMessage()` ) – KarelG Apr 20 '17 at 07:16
  • 2
    do you have access to the file, the file is already opened by another application? Show the exception message. – fubo Apr 20 '17 at 07:17
  • Which line is throwing Exception? Second line or forth line? – Almett Apr 20 '17 at 08:11
  • 2
    Off-topic: What you're doing here seems like a very bad idea. You are "leaking" an `IDisposable` out of its `using` block. That is, after the `using` block, `stream` (and therefore `attachment`) will have been disposed and you may no longer use `attachment`. – stakx - no longer contributing Apr 20 '17 at 08:15
  • are you reading local filesystem in asp.net? – Lei Yang Apr 20 '17 at 08:37
  • Possible duplicate of [ASP.NET Core serving a file outside of the project directory](http://stackoverflow.com/questions/43391812/asp-net-core-serving-a-file-outside-of-the-project-directory) – Set Apr 20 '17 at 10:40

1 Answers1

0

I had the same issue on one of my projects. I changed the Stream to FileStream:

using (var stream = new FileStream(yourPath, FileMode.Open))

Hope it helps

The J
  • 41
  • 8