mjyazdani
mjyazdani

Reputation: 2035

Close file after ReadAllBytes

I want to convert a file to byte[] and then save it. but while saving it says that file is in use. how should I close the file after reading it as byte?

  void Function(string UploadPath, RadAsyncUpload RadAsyncUploadName)
    {
        foreach (UploadedFile file in RadAsyncUploadName.UploadedFiles)
        {
            FileStream tmpFile = (FileStream)file.InputStream;
            byte[] data = File.ReadAllBytes(tmpFile.Name);
            //close the file here
            Function2(data);
            file.SaveAs(Server.MapPath(UploadPath)+file.GetName());
        }
    }

Upvotes: 0

Views: 2088

Answers (2)

Tarik
Tarik

Reputation: 11209

Once you have read all the bytes in the stream, it looks like you cannot use the UploadedFile.SaveAs anymore. I would first SaveAs, then open the saved file an read it. Alternatively, you can write back the byte array to the file you intended to save the data to.

Upvotes: 1

xpda
xpda

Reputation: 15813

You don't need to define the filestream -- just use the file name with readallbytes.

Upvotes: 1

Related Questions