user1328147 user1328147 - 1 year ago 206
C# Question

Access Denied When Creating a new File Using Filestream - Xamarin IOS

I am trying to create an empty PDF file in Xamarin.IOS using Filestream. When I try to create the Filestream using the following code:

FileStream fs = new FileStream("InvestmentAgreemen.pdf", FileMode.Create, FileAccess.ReadWrite);

I get the following error: "Access to the path "/private/var/containers/Bundle/Application/05AA1616-B66B-483D-8BA1-80A2B1AEC973/" is denied."

It worked fine when running on the iPad simulator, but as soon as I moved it to a real device I got this permissions error. Additionally, I have to use a filestream to create the empty PDF because I am also using Syncfusion's PDF tools for xamarin.IOS which require the use of Filestream for saving.
I also tried creating a FileIOPermission object granting AllAccess to AllFiles but I got a system not allowed error.
How can I resolve this permissions error?

Answer Source

You are trying to create a file in the root of your application's bundle, which is read-only. I'm surprised that this didn't fail on the simulator also.

To create a file at runtime, you need to specify a user writable path

var documents =
 Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); 
var filename = Path.Combine (documents, "InvestmentAgreemen.pdf");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download