0

I just uploaded a file to the wwwroot/files folder of my project. What I intend to do is read the file immediately after uploading it. The code I have used to upload the file works fine but the one for reading the file throws this Exception: An unhandled exception occurred while processing the request. IOException: The process cannot access the file 'campuses.sql' because it is being used by another process. Campuses.sql is the file I'm trying to upload and read. Below is my code for uploading and reading the file.

 public IActionResult Institution(IFormFile InstitutionFile)
    {
        if(InstitutionFile != null)
        {
            var fileName = Path.Combine(he.WebRootPath + "/files/", Path.GetFileName(InstitutionFile.FileName));
            InstitutionFile.CopyTo(new FileStream(fileName, FileMode.Create));

       //Everything runs fine till here

            int counter = 0;
            string line;

            // Read the file and display it line by line.  
            System.IO.StreamReader file =
                new System.IO.StreamReader(fileName);
            while ((line = file.ReadLine()) != null)
            {
                System.Console.WriteLine(line);
                counter++;
            }

            file.Close();

            System.Console.ReadLine();



        }

        return RedirectToAction("Index", "Institutions");

    }

So: 1. I suspect it is the upload process which is still using the file. How can I stop the process using the file so that I can read it?

  1. Is there another way that can help me go round this?

I intend to read data from an .csv file and insert the records into a database in the long run.

Thanks!

Caleb Rotich
  • 603
  • 5
  • 11
  • 1
    You're using a `FileStream` and not disposing it (actually you're not disposing any of your *streams*). You should always make sure you dispose instances that implement `IDisposable` in order to free up unmanaged resources. Checking [this answer](https://stackoverflow.com/a/26741192/967736) – IPValverde Mar 01 '18 at 06:46
  • Thanks, your comment helped a lot. @IPValverde – Caleb Rotich Mar 02 '18 at 09:29

1 Answers1

1

Read from InstitutionFile instead of physical file.

Also put FileStream into using block so it will release the file.

var fileName = Path.Combine("" + "/files/", Path.GetFileName(InstitutionFile.FileName));
using (var fileStream = new FileStream(fileName, FileMode.Create))
      InstitutionFile.CopyTo(fileStream);

var st = new MemoryStream();
InstitutionFile.CopyTo(st);
var content = Encoding.ASCII.GetString(st.ToArray());
tchelidze
  • 8,050
  • 1
  • 29
  • 49
  • It might work but the problem is the file will still not be available to other processes which might want to use it – Caleb Rotich Mar 01 '18 at 06:57
  • 1
    @CalebRotich after `FileStream` will finish the writing and goes out of scope (at the end of action) it will release the file. – tchelidze Mar 01 '18 at 06:58
  • Putting FileStream into Using block worked, thanks. Everything else remained constant though. – Caleb Rotich Mar 02 '18 at 09:27