Path of my file in project

Hi,
This is probably stupid question but I am going to ask it anyway. I want to find a path to a file that I import in my projects root directory. I cant manage to understand where it goes after build. My file name is test.xls, its Build Action is Content, and Copy to Output Directory is Copy Always. Trying to access it with this code doesnt seem to work:
System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "test.xls")

I tried this too, but that isnt good either:
System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "test.xls")

Can someone tell me what am I doing wrong?

Tagged:

Best Answer

Answers

  • momemome Member ✭✭

    @Charwaka said:
    Hi @misko101

    Please copy your "test.xls" to Assets folder in you application and set following properties

    Right Click on file > Properties > Build Action > AndroidAsset

    https://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_6_-_using_android_assets/Images/asset-properties-vs.png

    AssetManager assets = Android.App.Application.Context.Assets;
    var asset = Android.App.Application.Context.Assets.Open ("test.xls");
    

    Or If you want access from pcl you can do like this

    you'll need to add your file to your Forms PCL project, with a Build Action of EmbeddedResource

    Stream stream = this.GetType().Assembly.GetManifestResourceStream("<namespac‌​e>.test.xls");

    WOW!!! Well Done and thanks so much, you made my day.... I search 3-4hours for accessing but could not before trying this one!

  • DeepakDYDeepakDY INMember ✭✭✭

    @Charwaka said:
    Hi @misko101

    Please copy your "test.xls" to Assets folder in you application and set following properties

    Right Click on file > Properties > Build Action > AndroidAsset

    https://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_6_-_using_android_assets/Images/asset-properties-vs.png

    AssetManager assets = Android.App.Application.Context.Assets;
    var asset = Android.App.Application.Context.Assets.Open ("test.xls");
    

    Or If you want access from pcl you can do like this

    you'll need to add your file to your Forms PCL project, with a Build Action of EmbeddedResource

    Stream stream = this.GetType().Assembly.GetManifestResourceStream("<namespac‌​e>.test.xls");

    Yes worked for me:)

  • AndreasSelleAndreasSelle DEBeta ✭✭

    Hi,

    we are sharing a library that is built targeting ".NET Standard 2.0" across all platforms. It contains a file with the build action "Copy to Output Directory". As said above, unfortunately, this file does never appear anywhere in the output when we use the library from an Android project and the library does not work. Is there any way to build a ".NET Standard 2.0" library that accesses "Copy to Output Directory"-application-files in a platform independent way?

  • CharwakaCharwaka INMember ✭✭✭✭✭

    @AndreasSelle said:
    Hi,

    we are sharing a library that is built targeting ".NET Standard 2.0" across all platforms. It contains a file with the build action "Copy to Output Directory". As said above, unfortunately, this file does never appear anywhere in the output when we use the library from an Android project and the library does not work. Is there any way to build a ".NET Standard 2.0" library that accesses "Copy to Output Directory"-application-files in a platform independent way?

    Can you make Build Action as Embeded Resource and try

Sign In or Register to comment.