0
FileStream fileStream = File.Open(pdfPath, FileMode.Create);
stream.Position = 0;
stream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();

I also use this but still give me the same error

File.WriteAllBytes(pdfPath, bArray);
Jasmin Sojitra
  • 1,090
  • 10
  • 24
  • You could use the following code `using(FileStream sourceStream = File.Open(pdfPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using(FileStream destinationStream = File.Create(new path)) { await sourceStream.CopyToAsync(destinationStream); }` – Lucas Zhang Dec 20 '19 at 13:39
  • I try this but still give the same error. – Jasmin Sojitra Dec 23 '19 at 05:17

1 Answers1

2

This is a common C# problem, so if you understand it for C#, you will be able to implement it in your application as well.

The error is caused when you try to Read or Write a file you just created from a separate stream. Solving this is very simple, just dispose the filestream you used in creating it and then you can write to the file freely.

There's many ways of solving this. Since it seems like you don't just want to create the file, but also write to it, you should probably use:

FileStream fileStream = new FileStream(pdfPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

You can use these two links as references: from Xamarin and from Stack Overflow.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saamer
  • 4,687
  • 1
  • 13
  • 55