0

I open a file with write permission. But when i want to open again a file with the same name i get an Exception. Here the code:

// Check to see if file was uploaded
            if (FileMdl.PostedFile != null)
            {
                // Get a reference to PostedFile object
                HttpPostedFile myFile = FileMdl.PostedFile;

                // Get size of uploaded file
                int nFileLen = myFile.ContentLength;

                // make sure the size of the file is > 0
                if (nFileLen > 0)
                {
                    // Allocate a buffer for reading of the file
                    byte[] myData = new byte[nFileLen];

                    // Read uploaded file from the Stream
                    myFile.InputStream.Read(myData, 0, nFileLen);

                    // Create a name for the file to store
                    Constantes.strFilenameMdl = Path.GetFileName(myFile.FileName);

                    // Write data into a file
                    WriteToFile(Server.MapPath("Files//" + Constantes.strFilenameMdl), ref myData);
                    myFile.InputStream.Flush();
                    myFile.InputStream.Close();


                }
            }


    private void WriteToFile(string strPath, ref byte[] Buffer)
    {
        // Create a file
        FileStream newFile = new FileStream(strPath, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);

        // Write data to the file
        newFile.Write(Buffer, 0, Buffer.Length);

        // Close file
        newFile.Flush();
        newFile.Close();
        newFile.Dispose();
    }

Supposedly with Flush or Close or Dispose should work, but not so. Any idea?

Esneyder
  • 471
  • 1
  • 5
  • 19
  • Please search this site for the exact error message. You'll find dozens of results, one of which is certainly a solution to your problem. Start with any of the 8 in the 10 related questions to the right of yours. ======>>>>>> [FIleStream can't access the file because it's being used by another process](http://stackoverflow.com/q/13297466/62576) seems particularly appropriate as a starting point. – Ken White Sep 06 '13 at 22:23

2 Answers2

0

Before writing to file have you tried to close previous Stream:

                myFile.InputStream.Flush();
                myFile.InputStream.Close();

              // Write data into a file
                WriteToFile(Server.MapPath("Files//" + Constantes.strFilenameMdl), ref myData);
user007
  • 1,122
  • 1
  • 10
  • 30
0

Try to use using statement like this way:

    private void WriteToFile(string strPath, ref byte[] Buffer)
    {
        // Create a file
        using (FileStream newFile = new FileStream(strPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            // Write data to the file
            newFile.Write(Buffer, 0, Buffer.Length);
        }
    }

See more at: http://msdn.microsoft.com/en-us/library/vstudio/yh598w02(v=vs.100).aspx

Note: You can also simplify your code by remove the ref key out of input parameter and FileShare.ReadWrite parameter out of FileStream statement like this:

    private void WriteToFile(string strPath, byte[] Buffer)
    {
        // Create a file
        using (FileStream newFile = new FileStream(strPath, FileMode.Create, FileAccess.ReadWrite))
        {
            // Write data to the file
            newFile.Write(Buffer, 0, Buffer.Length);
        }
    }
Huy Nguyen
  • 918
  • 1
  • 10
  • 11