0

I'm using Xamarin.IOS to create an app that generates a pdf file. There are a couple pages that I want to add in to every file that gets generated. To achieve this, I've added the a folder called MyDocs to my project, and put File.pdf inside. I also added a class into MyDocs called FileGet, which has one static method called GetFile that returns a stream to File.pdf.

GetFile is implemented like so:

using System.IO;
...

return new FileStream(Path.Combine("MyDocs", "File.pdf"), FileMode.Open, FileAccess.Read);

and I call the method like so:

using MyApp.MyDocs;
using System.IO;
/*
code to generate the user's pdf document
*/

FileStream stream = FileGet.GetFile();

/*
code to append documents
*/

but get a

System.IO.DirectoryNotFound: Could not find a part of the path "/private/var/containers/Bundle/Application/1B01EF3D-CA49-47F9-9CBB-30EE10AEE6A1/MyApp.app/MyDocs/File.pdf".

What's the proper way to access File.pdf within my MyDocs folder? or is this not the proper way to store a pdf file within the app?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
jarjar27
  • 117
  • 1
  • 14

1 Answers1

1

First make sure that your files have a Build Action of BundleResource

|- MyDocs
|   |- SomeBundledFile.png (Build Action = BundleResource)

So assuming the file(s) are in your Xamarin.iOS project are in the above structure, you can use the NSBundle.MainBundle.BundlePath to obtain the root path and then add the rest:

var path = Path.Combine(NSBundle.MainBundle.BundlePath, "MyDocs", "SomeBundledFile.png");
using (var stream = new FileStream(path, FileMode.Open))
{
    Console.WriteLine(stream.Length);
}

Also some classes have static initializers that take bundled resources into consideration:

var image = UIImage.FromBundle("MyDocs/SomeBundleFile.png");
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • setting the Build Action to BundleResource was something I had not done yet, but doing so and using NSBundle.MainBundle.BundlePath only ends up giving me a slightly different path in my DirectoryNotFoundException – jarjar27 May 19 '18 at 23:26
  • @jarjar27 Double check the case of your directory (and filename), the file system is case-sensitive) – SushiHangover May 19 '18 at 23:32
  • I can promise you thats not the issue. Can you take a look at the path given in the error report to see if I'm missing something? – jarjar27 May 20 '18 at 16:42
  • @jarjar27 You are not running this on Xamarin Live are you? – SushiHangover May 20 '18 at 16:50
  • It's an IPad app so I'm running it on my IPad which is physically connected to my Mac. I had it working how I had it implemented, but then I added more files to the folder to pull in and it all stopped – jarjar27 May 20 '18 at 16:59
  • @jarjar27 Delete the app via Springboard and have VS4M re-deploy it to the device and retest it – SushiHangover May 20 '18 at 17:00
  • Beautiful. Thank you so much! So when modifying the structure of project folders/files is it best to delete and reinstall the app? – jarjar27 May 20 '18 at 17:11
  • 1
    @jarjar27 You should not **have** too, it might be because the files were not originally marked as BundleResource and msbuild did not see the change(?)... but when in doubt with Xamarin/msbuild, manually delete and try again ;-) – SushiHangover May 20 '18 at 17:23