0
votes

I am currently using FilePicker in xamarin forms to choose a zip file, then I get fullpath from it and use this fullpath in Xamarin.Android project. Using following code:

            var result = await FilePicker.PickAsync(new PickOptions
            {

                PickerTitle = "Select zip file"
            });

            if (result != null)
            {
                FileNameLabel.Text = $"{result.FileName}";
                if (!result.FileName.EndsWith("zip", StringComparison.OrdinalIgnoreCase))
                {
                    await DisplayAlert("Wrong File", "Selected file is not right", "Ok");
                }
                else
                {
                    var stream = result.OpenReadAsync();
                    _filepath = result.FullPath;
                }

            }

I want to do the same thing, but without FilePicker, instead add my zip file as embedded resource. I know I can access, embedded resouce using following code, but I am unable to get FullPath.

        var assembly = IntrospectionExtensions.GetTypeInfo(typeof(App)).Assembly;
        Stream stream = assembly.GetManifestResourceStream("TestFile.json");

is it possible to do something like:

         File file = new File(assembly.GetManifestResourceStream("TestFile.Zip"));
        _fullname = file.FullName;
1
embedded resources are not files and do not have file pathsJason
Then is it logical to move embedded resource to cache and try to get Fullpath?Umer Mehmood

1 Answers

1
votes

Yes,you could use Xamarin.Essentials to obtain the OS's cache directory and then copy the embedded resource to it.

For example:

var cacheFile = Path.Combine(FileSystem.CacheDirectory, "your file");
if (File.Exists(cacheFile))
    File.Delete(cacheFile);
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("your embedded resource"))
using (var file = new FileStream(cacheFile, FileMode.Create, FileAccess.Write))
 {
   resource.CopyTo(file);
 }